Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python'的名称是什么;谁是根记录器?_Python_Logging - Fatal编程技术网

Python'的名称是什么;谁是根记录器?

Python'的名称是什么;谁是根记录器?,python,logging,Python,Logging,下面是关于登录Python的基本教程,以及iPython控制台中的步骤。如上所述,根记录器的默认级别为警告: 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.g

下面是关于登录Python的基本教程,以及iPython控制台中的步骤。如上所述,根记录器的默认级别为
警告

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()
,以获取根记录器?

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

检查表明:

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 = {}
root
loggerDict
分开存储,这是所有命名记录器所在的位置

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
内部函数查找它无法找到的
根目录

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

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