找不到logger\uu main的处理程序__

2024-04-29 01:10:42 发布

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

我使用logconfig.ini文件在python包中设置了日志记录。

[loggers]
keys=extracts,root

[formatters]
keys=simple,detailed

[handlers]
keys=file_handler

[formatter_simple]
format=%(module)s - %(levelname)s - %(message)s
datefmt=%Y-%m-%d %H:%M:%S

[formatter_detailed]
format=%(asctime)s %(name)s:%(lineno)s %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S

[handler_file_handler]
class=logging.handlers.RotatingFileHandler
level=DEBUG
formatter=detailed
args=('/ebs/logs/foo.log', 'a', 100000000, 3)

[logger_extracts]
level=DEBUG
handlers=file_handler
propagate=1
qualname=extracts

[logger_root]
level=NOTSET
handlers=

但每当我运行我的应用程序时,我都会在提示符下收到以下警告消息

No handlers found for logger __main__

我该怎么解决?


Tags: formatmessageformatterhandlersrootkeysloggersimple
2条回答

必须先调用logging.basicConfig():

Logging HOWTO

The call to basicConfig() should come before any calls to debug(), info() etc. As it’s intended as a one-off simple configuration facility, only the first call will actually do anything: subsequent calls are effectively no-ops.

或自动调用logging.basicConfig()的所有logging.info('Starting logger for…')。比如说:

import logging
logging.info('Starting logger for...') # or call logging.basicConfig()
LOG = logging.getLogger(name)

模块作者执行此行为的原因是here

我发现了我的错误。 原来根日志用于main。 我只需要在根日志中附加一个处理程序

[logger_root]
level=NOTSET
handlers=file_handler

相关问题 更多 >