Pyhton的logging功能非常强大,附上平时经常使用的一段代码
这个open_logging的作用有2个:
1.命令窗口输出log;
2.将log写入到文件中;
# -*- coding:UTF-8 -*- __author__ = 'gancj' __data__ = '2015-05-30 11:28' __mail__ = '[email protected]/[email protected]' import logging import time def get_now_time(): today_date = str(time.strftime('%Y-%m-%d-%H',time.localtime(time.time()))) return today_date def open_logging(): logging.basicConfig(level=logging.NOTSET, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='{}applicationrunning.log'.format(get_now_time()), filemode='a') #日志级别CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET,数值大小是,50,40,...0,当然也可以自己定义日志级别,自己输入数字。 console = logging.StreamHandler() console.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s') console.setFormatter(formatter) logging.getLogger('').addHandler(console)