我想对我的应用程序进行日志级跟踪(5),因为我认为debug()
还不够。另外log(5, msg)
不是我想要的。如何向Python记录器添加自定义日志级别?
我有一个mylogger.py
包含以下内容:
import logging
@property
def log(obj):
myLogger = logging.getLogger(obj.__class__.__name__)
return myLogger
在我的代码中,我使用它的方式如下:
class ExampleClass(object):
from mylogger import log
def __init__(self):
'''The constructor with the logger'''
self.log.debug("Init runs")
现在我想打电话给self.log.trace("foo bar")
提前谢谢你的帮助。
编辑(2016年12月8日):我将接受的答案改为pfa's,即IMHO,这是一个基于Eric S非常好的建议的优秀解决方案
结合所有现有的答案和大量的使用经验,我想我已经列出了所有需要做的事情,以确保完全无缝地使用新的级别。以下步骤假设您正在添加值为
logging.DEBUG - 5 == 5
的新级别TRACE
:logging.addLevelName(logging.DEBUG - 5, 'TRACE')
需要调用才能在内部注册新级别,以便可以按名称引用它。logging
本身以保持一致性:logging.TRACE = logging.DEBUG - 5
。trace
的方法添加到logging
模块中。它的行为应该像debug
,info
等trace
的方法添加到当前配置的记录器类中。由于不能百分之百保证是logging.Logger
,请改用logging.getLoggerClass()
。以下方法说明了所有步骤:
我接受了“避免看到lambda”的答案,并且不得不修改添加我的日志级别的日志的位置。我也看到了保罗所做的“我认为这不管用”的问题。您不需要logger作为日志级别的第一个参数吗?”这对我有效
@埃里克S
Eric S.的回答很好,但我通过实验了解到,无论日志级别设置为什么,这都会导致在新的调试级别记录的消息被打印出来。因此,如果将
9
设为新的级别号,如果调用setLevel(50)
,则会错误地打印较低级别的消息。修复了检查日志记录级别是否已启用的示例:
如果您在Python 2.7的
logging.__init__.py
中查看class Logger
的代码,这就是所有标准日志函数所做的工作(关键,.debug等)。很明显我不能回复别人的回复,因为我的名声不好。。。如果埃里克看到这个,希望他能更新他的帖子。=)
相关问题 更多 >
编程相关推荐