码迷,mamicode.com
首页 > 移动开发 > 详细

[Python] Create a Log for your Python application

时间:2017-12-11 16:10:04      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:col   python   sys   []   debug   number   stat   ons   nts   


Print statements will get you a long way in monitoring the behavior of your application, but logging will get your further. Learn how to implement logging in this lesson to generate INFO, WARNING, ERROR, and DEBUG logs for your application.

 

 

import sys
import getopt
import logging

# pass in: python3 my_log.py -l info

# Get command line options
# short: l:
# long: [log=]
opts, args = getopt.getopt(sys.argv[1:], "l:", ["log="])

print("opts", opts) #[(‘-l‘, ‘info‘)]
print("args", args) #[]

# default log level
log_level="INFO"

for opt, arg in opts: #opt: -l, arg: info
    if opt in ("-l", "--log"):
        log_level = getattr(logging, arg.upper())

logging.basicConfig(filename="./demo.log", level=log_level, format=%(asctime)s %(levelname)s:%(message)s)


for i in range(0, 100):
    if i % 5 == 0:
        logging.debug(Found a number divisible by 5: {0}.format(i))
    else:
        logging.info(At number {0}.format(i))

logging.warning(Finished sequence)

 

[Python] Create a Log for your Python application

标签:col   python   sys   []   debug   number   stat   ons   nts   

原文地址:http://www.cnblogs.com/Answer1215/p/8023103.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!