【问题标题】:What is the name of Python's root logger?Python 的根记录器的名称是什么?
【发布时间】:2018-02-09 18:42:31
【问题描述】:

我正在学习有关登录 Python 的基本教程https://docs.python.org/3/howto/logging.html#a-simple-example,并按照 iPython 控制台中的步骤进行操作。如那里所述,根记录器的默认级别是WARNING

In [1]: import logging

In [2]: logging.warning('Watch out!')
WARNING:root:Watch out!

In [3]: logging.info('I told you so')

我想将根记录器的级别显式设置为INFO。我尝试使用name='root' 进行操作,但这没有明显效果:

In [4]: logging.getLogger('root').setLevel(logging.INFO)

In [5]: logging.info('I told you so')

如果我不带参数调用logging.getLogger(),我可以设置根记录器的级别:

In [6]: logging.getLogger().setLevel(logging.INFO)

In [7]: logging.info('I told you so')
INFO:root:I told you so

不过,我很好奇,为什么第一次没有成功?看起来这应该可以工作,因为它的name 属性是'root':

In [12]: root_logger = logging.getLogger()

In [14]: root_logger.name
Out[14]: 'root'

简而言之,如果我不想依赖默认值,我会将什么名称传递给 logging.getLogger() 以获取根记录器?

【问题讨论】:

  • 根记录器有一个name 属性,但它没有与您定义的其他记录器一起存储在字典中,因此无法通过名称通过getLogger() 访问

标签: python logging


【解决方案1】:

根记录器的名称是root,但您不能通过名称访问根记录器。

检查logging.py 揭示了这一点:

def __init__(self, root):
    """
    Initialize the manager with the root node of the logger hierarchy.
    """
    self.root = root
    self.disable = 0
    self.emittedNoHandlerWarning = 0
    self.loggerDict = {}

rootloggerDict 分开存储,这是所有命名记录器所在的位置。

def getLogger(name=None):
    """
    Return a logger with the specified name, creating it if necessary.
    If no name is specified, return the root logger.
    """
    if name:
        return Logger.manager.getLogger(name)
    else:
        return root

你可以关注manager.getLogger内部函数,发现找不到root

def getLogger(self, name):
        """
        Get a logger with the specified name (channel name), creating it
        if it doesn't yet exist. If a PlaceHolder existed for the specified
        name [i.e. the logger didn't exist but a child of it did], replace
        it with the created logger and fix up the parent/child references
        which pointed to the placeholder to now point to the logger.
        """
        rv = None
        _acquireLock()
        try:
            if self.loggerDict.has_key(name):
                rv = self.loggerDict[name]
                if isinstance(rv, PlaceHolder):
                    ph = rv
                    rv = _loggerClass(name)
                    rv.manager = self
                    self.loggerDict[name] = rv
                    self._fixupChildren(ph, rv)
                    self._fixupParents(rv)
            else:
                rv = _loggerClass(name)
                rv.manager = self
                self.loggerDict[name] = rv
                self._fixupParents(rv)
        finally:
            _releaseLock()
        return rv

【讨论】:

    【解决方案2】:

    root logger 被访问:

    logging.getLogger('')
    

    示例代码:

    logging.warning('Watch out!')
    logging.getLogger('').setLevel(logging.INFO)
    logging.info('I told you so')
    

    输出:

      WARNING:root:Watch out!
      INFO:root:I told you so
    

    【讨论】:

    • 它更常见(您的方式可能工作)以logging.getLogger() 访问,因为 OP 已经知道...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 2014-04-09
    • 2020-01-07
    • 2020-05-08
    • 2015-04-10
    • 1970-01-01
    相关资源
    最近更新 更多