如何在python中创建非root记录器 [英] How to create a non-root logger in python

查看:63
本文介绍了如何在python中创建非root记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用logging.getLogger('child')在子python模块中创建非root记录器,但出现错误找不到记录器"child"的处理程序"

I'm trying to create a non-root logger in a child python module using logging.getLogger('child'), but I'm getting an error "No handlers could be found for logger "child"

我正在尝试将父模块日志记录到父日志文件中.父模块将创建子模块的实例,并且我希望子模块编写自己的子日志文件,而不将其消息传播到父日志文件.

I'm trying to have a parent module log to parent log file. The parent module will create instances of the child module, and I want the child module to write its own child log file, without propagating its messages up to the parent log file.

这是我为父模块准备的(这是由用户执行的):

Here's what I have for the parent module (this is executed by the user):

#!/usr/bin/env python

import logging, child

logging.basicConfig( filename='parent.log' )
logging.warning( 'test log from parent' )

c = child.run()

这是子模块:

import logging
class run:
  def __init__(self):
    logging.basicConfig( filename = 'child.log' )
    childLogger = logging.getLogger( __name__ )
    childLogger.propagate = False
    childLogger.warning( 'this is a log from the child' )

预期的输出将包含2个文件:parent.log(包含来自父模块的1条警告行)和child.log(包含来自子模块的1条警告行).

Expected output is to have 2 files: parent.log (containing the 1 WARNING line from the parent module) and child.log (containing the one WARNING line from the child module).

实际输出是:parent.log文件中打印了一条(来自父级的)警告行,并且没有child.log文件-子日志消息未记录在任何地方.

Actual output is: one WARNING line (from the parent) is printed into the parent.log file, and there is no child.log file--the child log message isn't recorded anywhere.

您能告诉我我想念的东西吗?TIA!

Can you please tell me what I'm missing? TIA!

推荐答案

替换

logging.basicConfig(filename='child.log')
childLogger = logging.getLogger(__name__)

在您的子模块中使用:

childLogger = logging.getLogger(__name__)
childLogger.addHandler(logging.FileHandler('child.log'))

,或者使用 dictConfig fileConfig 来在一处配置日志记录.

or, alternatively, use dictConfig or fileConfig to configure logging in one place.

basicConfig 有什么问题?从文档中:

What's wrong with basicConfig? From the docs:

如果根记录器已经为其配置了处理程序,则此功能将不执行任何操作.

This function does nothing if the root logger already has handlers configured for it.

因此,基本上,您在子模块中对 basicLogging 的调用无效,因为第一个(在父模块中)已经配置了根记录器.通过将子记录器的 propagate 设置为 false ,子记录器的日志条目不会转发到根记录器,因此您得到了找不到处理程序... 警告.

So basically, your call to basicLogging in child module has no effect, since the first one (in parent module) has already configured the root logger. And by setting the child logger's propagate to false, the child's log entries don't get forwarded to the root logger, so you get the No handlers could be found... warning.

修改

仅需详细说明 fileConfig ,这提供了很大的灵活性,对于您的模块,您可以创建一个名为 logging.ini 的文件:

Just to elaborate on fileConfig, which allows a lot of flexibility, for your modules you could create a file called logging.ini:

[loggers]
keys=root,child

[handlers]
keys=logfile,logfile_child

[formatters]
keys=default

# loggers

[logger_root]
level=INFO
handlers=logfile

[logger_child]
level=INFO
handlers=logfile_child
qualname=child
propagate=0

# handlers

[handler_logfile]
class=FileHandler
formatter=default
args=('parent.log',)

[handler_logfile_child]
class=FileHandler
formatter=default
args=('child.log',)

# formatters

[formatter_default]
format=%(asctime)s [%(module)s:%(lineno)d] %(levelname)s %(message)s
datefmt=

然后,在应用程序中的某个地方,您只需要调用 fileConfig :

Then, somewhere in your application, you would just need to call fileConfig:

import logging.config

logging.config.fileConfig('logging.ini')

通过这种方式,您的日志记录可在一个位置进行配置,从而使添加其他记录器,更改日志级别等更加容易.

This way your logging is configured in one place, which makes is easier to add additional loggers, change log levels, etc.

这篇关于如何在python中创建非root记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆