我正在尝试在一个子Python模块中使用logging.getLogger('child')创建一个非根记录器,但是我收到了一个错误消息“找不到适合“child”记录器的处理程序”。
我想让父模块记录到父日志文件。父模块将创建子模块的实例,我希望子模块能够编写自己的子日志文件,而不会将其消息传播到父日志文件。
以下是我为父模块准备的代码(由用户执行):
我想让父模块记录到父日志文件。父模块将创建子模块的实例,我希望子模块能够编写自己的子日志文件,而不会将其消息传播到父日志文件。
以下是我为父模块准备的代码(由用户执行):
#!/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' )
期望的输出结果是有两个文件:parent.log(包含父模块中的1个警告行)和child.log(包含子模块中的一个警告行)。
实际输出结果是:只有一个来自父模块的警告行被打印到parent.log文件中,而没有child.log文件——子日志消息没有被记录在任何地方。
请问我错过了什么?谢谢!