相关疑难解决方法(0)

如何将自定义日志级别添加到Python的日志记录工具中

我想为我的应用程序提供loglevel TRACE(5),因为我认为这debug()还不够.另外log(5, msg)不是我想要的.如何将自定义日志级别添加到Python记录器?

我有mylogger.py以下内容:

import logging

@property
def log(obj):
    myLogger = logging.getLogger(obj.__class__.__name__)
    return myLogger
Run Code Online (Sandbox Code Playgroud)

在我的代码中,我以下列方式使用它:

class ExampleClass(object):
    from mylogger import log

    def __init__(self):
        '''The constructor with the logger'''
        self.log.debug("Init runs")
Run Code Online (Sandbox Code Playgroud)

现在我想打个电话 self.log.trace("foo bar")

在此先感谢您的帮助.

编辑(2016年12月8日):我改变了接受pfa的答案,即恕我直言,这是一个很好的解决方案,基于Eric S.的非常好的建议.

python logging python-logging

102
推荐指数
10
解决办法
4万
查看次数

标签 统计

logging ×1

python ×1

python-logging ×1