gpt4 book ai didi

python - 没有找到记录器 __main__ 的处理程序

转载 作者:太空狗 更新时间:2023-10-29 17:44:02 25 4
gpt4 key购买 nike

我使用 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__

我该如何解决这个问题?

最佳答案

您必须先调用 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

关于python - 没有找到记录器 __main__ 的处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27411778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com