logging.debug()
, logging.info()
等方法的定义中,除了msg和args参数外,还有一个**kwargs参数。
它们支持3个关键字参数: exc_info
, stack_info, extra
,下面对这几个关键字参数作个说明。
关于exc_info, stack_info, extra
关键词参数的说明:
(1)exc_info
其值为布尔值,如果该参数的值设置为True,则会将异常异常信息添加到日志消息中。如果没有异常信息则添加None到日志信息中。
(2)stack_info
其值也为布尔值,默认值为False。如果该参数的值设置为True,栈信息将会被添加到日志信息中。
(3)extra
这是一个字典(dict)参数,它可以用来自定义消息格式中所包含的字段,但是它的key不能与logging模块定义的字段冲突
一个例子:在日志消息中添加exc_info
和stack_info
信息,并添加两个自定义的字端 ip和user
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(user)s[%(ip)s] - %(message)s"
DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p"
logging.basicConfig(format=LOG_FORMAT, datefmt=DATE_FORMAT)
logging.warning("Some one delete the log file.", exc_info=True, stack_info=True, extra={'user': 'Tom', 'ip':'47.98.53.222'})
输出结果:
05/08/2017 16:35:00 PM - WARNING - Tom[47.98.53.222] - Some one delete the log file.
NoneType
Stack (most recent call last):
File "C:/Users/wader/PycharmProjects/LearnPython/day06/log.py", line 45, in <module>
logging.warning("Some one delete the log file.", exc_info=True, stack_info=True, extra={'user': 'Tom', 'ip':'47.98.53.222'})
网友评论