设为首页 加入收藏

TOP

Python logging模块详解(一)
2015-02-02 14:33:05 来源: 作者: 【 】 浏览:72
Tags:Python logging 模块 详解

简单将日志打印到屏幕:


import logging
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')


输出:


WARNING:root:warning message
ERROR:root:error message
CRITICAL:root:critical message


可见,默认情况下python的logging模块将日志打印到了标准输出中,且只显示了大于等于WARNING级别的日志,这说明默认的日志级别设置为WARNING(日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET),默认的日志格式为日志级别:Logger名称:用户输出消息。


灵活配置日志级别,日志格式,输出位置


import logging
logging.basicConfig(level=logging.DEBUG,
? ? ? ? ? ? ? ? ? ? format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
? ? ? ? ? ? ? ? ? ? datefmt='%a, %d %b %Y %H:%M:%S',
? ? ? ? ? ? ? ? ? ? filename='/tmp/test.log',
? ? ? ? ? ? ? ? ? ? filemode='w')


logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')


查看输出:
cat /tmp/test.log
Mon, 05 May 2014 16:29:53 test_logging.py[line:9] DEBUG debug message
Mon, 05 May 2014 16:29:53 test_logging.py[line:10] INFO info message
Mon, 05 May 2014 16:29:53 test_logging.py[line:11] WARNING warning message
Mon, 05 May 2014 16:29:53 test_logging.py[line:12] ERROR error message
Mon, 05 May 2014 16:29:53 test_logging.py[line:13] CRITICAL critical message


可见在logging.basicConfig()函数中可通过具体参数来更改logging模块默认行为,可用参数有
filename:用指定的文件名创建FiledHandler(后边会具体讲解handler的概念),这样日志会被存储在指定的文件中。
filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。
format:指定handler使用的日志显示格式。
datefmt:指定日期时间格式。
level:设置rootlogger(后边会讲解具体概念)的日志级别
stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件,默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。


format参数中可能用到的格式化串:
%(name)s Logger的名字
%(levelno)s 数字形式的日志级别
%(levelname)s 文本形式的日志级别
%(pathname)s 调用日志输出函数的模块的完整路径名,可能没有
%(filename)s 调用日志输出函数的模块的文件名
%(module)s 调用日志输出函数的模块名
%(funcName)s 调用日志输出函数的函数名
%(lineno)d 调用日志输出函数的语句所在的代码行
%(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
%(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数
%(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
%(thread)d 线程ID。可能没有
%(threadName)s 线程名。可能没有
%(process)d 进程ID。可能没有
%(message)s用户输出的消息



若要对logging进行更多灵活的控制有必要了解一下Logger,Handler,Formatter,Filter的概念
上述几个例子中我们了解到了logging.debug()、logging.info()、logging.warning()、logging.error()、logging.critical()(分别用以记录不同级别的日志信息),logging.basicConfig()(用默认日志格式(Formatter)为日志系统建立一个默认的流处理器(StreamHandler),设置基础配置(如日志级别等)并加到root logger(根Logger)中)这几个logging模块级别的函数,另外还有一个模块级别的函数是logging.getLogger([name])(返回一个logger对象,如果没有指定名字将返回root logger)


先看一个具体的例子


#coding:utf-8
import logging


# 创建一个logger?
logger = logging.getLogger()


logger1 = logging.getLogger('mylogger')
logger1.setLevel(logging.DEBUG)


logger2 = logging.getLogger('mylogger')
logger2.setLevel(logging.INFO)


logger3 = logging.getLogger('mylogger.child1')
logger3.setLevel(logging.WARNING)


logger4 = logging.getLogger('mylogger.child1.child2')
logger4.setLevel(logging.DEBUG)


logger5 = logging.getLogger('mylogger.child1.child2.child3')
logger5.setLevel(logging.DEBUG)


# 创建一个handler,用于写入日志文件?
fh = logging.FileHandler('/tmp/test.log')


# 再创建一个handler,用于输出到控制台?
ch = logging.StreamHandler()


# 定义handler的输出格式formatter?
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s

首页 上一页 1 2 3 4 5 6 7 下一页 尾页 1/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Python内置的urllib模块不支持htt.. 下一篇让shell脚本中的echo输出带颜色

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: