简体   繁体   English

如何在python中创建非根记录器

[英]How to create a non-root logger in python

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" 我正在尝试使用logging.getLogger('child')在子python模块中创建一个非root记录器,但是我收到一个错误“找不到logler”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()

And here's the child module: 这是儿童模块:

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' )

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). 预期的输出是2个文件:parent.log(包含父模块的1 WARNING行)和child.log(包含子模块中的一个WARNING行)。

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. 实际输出是:一个WARNING行(来自父级)打印到parent.log文件中,并且没有child.log文件 - 子日志消息不会记录在任何地方。

Can you please tell me what I'm missing? 你能告诉我我错过了什么吗? TIA! TIA!

Replace 更换

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

in your child module with: 在您的子模块中:

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

or, alternatively, use dictConfig or fileConfig to configure logging in one place. 或者,使用dictConfigfileConfig在一个地方配置日志记录。

What's wrong with basicConfig ? basicConfig有什么问题? From the docs: 来自文档:

This function does nothing if the root logger already has handlers configured for it. 如果根记录器已经为其配置了处理程序,则此函数不执行任何操作。

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. 所以基本上,你对子模块中的basicLogging的调用没有任何效果,因为第一个(在父模块中)已经配置了根记录器。 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. 通过将子记录器的propagate设置为false ,子项的日志条目不会被转发到根记录器,因此您No handlers could be found...警告。

Edit 编辑

Just to elaborate on fileConfig , which allows a lot of flexibility, for your modules you could create a file called logging.ini : 只需详细说明fileConfig ,它可以为您的模块提供很大的灵活性,您可以创建一个名为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=

Then, somewhere in your application, you would just need to call fileConfig : 然后,在应用程序的某个地方,您只需要调用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. 这样您的日志记录就可以在一个地方配置,这样可以更容易地添加其他记录器,更改日志级别等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从非 root 记录器记录 INFO 消息? - How to log INFO messages from the non-root logger? 如何使用 R + Anaconda3 + 非 root 用户创建 Dockerfile - How to create Dockerfile with R + Anaconda3 + non-root User 使用非root记录器获取所有级别的事件 - Getting events of all levels using a non-root logger 为什么非root记录器不向日志文件写入任何日志? - Why non-root logger is not writing any log to log file? 根记录器从非根记录器调用文件处理程序 - 我试图理解为什么 - root logger invokes filehandler from non-root logger - I'm trying to understand why 以非 root 用户身份运行 python 脚本 - Run python script as non-root user 如何用django作为非root运行gunicorn - how to run gunicorn with django as non-root Linux / Python如何将root访问py模块作为非r​​oot用户导入? - Linux/Python How do I import a root access py module as a non-root user? 如何将以非root用户身份安装在另一台服务器上的python安装重新定位到其他目录中的另一台服务器? - How to relocate python installation installed as non-root user on another server to another server in different directory? pip/poetry 如何在“src”等非根目录下找到 python 模块? - How can pip/poetry find the python modules under a non-root directory like 'src'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM