Python的Logging使用教程

来源:互联网 发布:java中的compare 编辑:程序博客网 时间:2024/06/06 03:50

Pyhton的logging功能非常强大,附上平时经常使用的一段代码

这个open_logging的作用有2个:

1.命令窗口输出log;

2.将log写入到文件中;

</pre><pre name="code" class="python"># -*- coding:UTF-8 -*-__author__ = 'gancj'__data__ = '2015-05-30 11:28'__mail__ = 'chaojiang.gcj@alibaba-inc.com/393037282@qq.com'import loggingimport timedef get_now_time():    today_date = str(time.strftime('%Y-%m-%d-%H',time.localtime(time.time())))    return today_datedef 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)


0 0
原创粉丝点击