Python 日志记录:使用 fileconfig 和编程配置之间的不同行为
我刚刚发现了 Python 日志记录的不同行为,具体取决于我是否使用文件配置日志记录和编程配置日志记录。
为了进行演示,我创建了两个最小的示例。
在第一个示例中,我以编程方式配置日志记录。此示例按预期工作 - 调试日志消息被打印到控制台。
# foo.py
import logging
logger = logging.getLogger(__name__)
class Foo(object):
def __init__(self):
logger.debug('debug log from Foo')
##########################################################################
# loggingtest.py
import logging.config
from foo import Foo
if __name__ == '__main__':
consoleLogger = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)-6s: %(name)s - %(levelname)s - %(message)s')
consoleLogger = logging.StreamHandler()
consoleLogger.setLevel(logging.DEBUG)
consoleLogger.setFormatter(formatter)
rootLogger = logging.getLogger()
rootLogger.addHandler(consoleLogger)
rootLogger.setLevel(logging.NOTSET)
# prints debug log message to console
foo = Foo()
在我的第二个示例中,我使用 fileConfig 配置日志记录。据我所知,日志配置文件应该具有完全相同的行为。但是,此示例中仍然没有打印调试日志消息。
# foo.py (same as above)
import logging
logger = logging.getLogger(__name__)
class Foo(object):
def __init__(self):
logger.debug('debug log from Foo')
##########################################################################
# loggingtest.py
import logging.config
from foo import Foo
if __name__ == '__main__':
logging.config.fileConfig('logging.cfg')
# does NOT print debug log message to console. WHY???
foo = Foo()
##########################################################################
# logging.cfg
[loggers]
keys = root
[logger_root]
level = NOTSET
handlers = consoleHandler
[formatters]
keys = complex
[formatter_complex]
format = %(asctime)s - %(name)s - %(levelname)s - %(module)s : %(lineno)d - %(message)s
[handlers]
keys = consoleHandler
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=complex
args=(sys.stdout,)
那么为什么使用日志文件配置的第二个示例不将我的调试日志消息打印到控制台呢?
I just discovered a different behavior of Python logging, depending on if I use logging with fileconfig and logging with programmatic config.
To demonstrate, I created two minimal examples.
In the first example I'm configuring logging programmatically. This example works as expected - the debug log message is printed to the console.
# foo.py
import logging
logger = logging.getLogger(__name__)
class Foo(object):
def __init__(self):
logger.debug('debug log from Foo')
##########################################################################
# loggingtest.py
import logging.config
from foo import Foo
if __name__ == '__main__':
consoleLogger = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)-6s: %(name)s - %(levelname)s - %(message)s')
consoleLogger = logging.StreamHandler()
consoleLogger.setLevel(logging.DEBUG)
consoleLogger.setFormatter(formatter)
rootLogger = logging.getLogger()
rootLogger.addHandler(consoleLogger)
rootLogger.setLevel(logging.NOTSET)
# prints debug log message to console
foo = Foo()
In my second example, I'm configuring logging with fileConfig. As far as I can see, the log-config-file should have the exact same behavior. But still, the debug log message is NOT printed in this example.
# foo.py (same as above)
import logging
logger = logging.getLogger(__name__)
class Foo(object):
def __init__(self):
logger.debug('debug log from Foo')
##########################################################################
# loggingtest.py
import logging.config
from foo import Foo
if __name__ == '__main__':
logging.config.fileConfig('logging.cfg')
# does NOT print debug log message to console. WHY???
foo = Foo()
##########################################################################
# logging.cfg
[loggers]
keys = root
[logger_root]
level = NOTSET
handlers = consoleHandler
[formatters]
keys = complex
[formatter_complex]
format = %(asctime)s - %(name)s - %(levelname)s - %(module)s : %(lineno)d - %(message)s
[handlers]
keys = consoleHandler
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=complex
args=(sys.stdout,)
So why is the second example using a logging fileconfig NOT printing my debug log message to the console?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
fileConfig
默认情况下禁用现有记录器,因此请调用before
或调用
Since
fileConfig
disables existing loggers by default, callbefore
or call