日志层次结构与根日志?

2024-06-26 14:43:00 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的代码中,有这样的东西:

logger = logging.getLogger('debug0.x')

按照我的理解,这应该只有在我以前做过类似的事情时才能做出反应:

logging.basicConfig(filename='10Nov2010a.txt',level=logging.DEBUG, name='debug0')

注意,name被定义为debug0。但是,我发现如果是的话

logging.basicConfig(filename='10Nov2010a.txt',level=logging.DEBUG)

如果不使用name关键字,则上面定义的debug0.x记录器会做出反应,并写入日志文件。我想只有在第一个案例中,当伐木工人被命名时,它才会有反应。

我很困惑。


Tags: 代码namedebugtxt定义logging关键字filename
2条回答

如果您签出代码或文档:

>>> print logging.basicConfig.__doc__

    Do basic configuration for the logging system.

    This function does nothing if the root logger already has handlers
    configured. ...............
    A number of optional keyword arguments may be specified, which can alter
    the default behaviour.

    filename  Specifies that a FileHandler be created, using the specified
              filename, rather than a StreamHandler.
    filemode  Specifies the mode to open the file, if filename is specified
              (if filemode is unspecified, it defaults to 'a').
    format    Use the specified format string for the handler.
    datefmt   Use the specified date/time format.
    level     Set the root logger level to the specified level.
    stream    Use the specified stream to initialize the StreamHandler. Note
              that this argument is incompatible with 'filename' - if both
              are present, 'stream' is ignored.

logging.basicConfig根本不使用name参数。它初始化根记录器。而getLogger采用“name”参数

>>> print logging.getLogger.__doc__

    Return a logger with the specified name, creating it if necessary.

    If no name is specified, return the root logger.

Pythonlogging模块按层次结构组织记录器。所有记录器都是根记录器的后代。每个记录器将日志消息传递到其父级。

使用getLogger()函数创建新的记录器。函数调用logging.getLogger('debug0.x')创建一个记录器x,它是debug0的子级,它本身是根记录器的子级。当记录到此记录器时,它将把消息传递给其父记录器,其父记录器将把消息传递给根记录器。您通过basicConfig()函数将根日志记录器配置为记录到文件,因此您的消息将在那里结束。

相关问题 更多 >