我可以吗日志.ini没有根记录程序的文件?

2024-06-10 22:22:41 发布

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

这是我的日志.ini文件看起来像:

[loggers]
keys=teja

[handlers]
keys=fileHandler

[formatters]
keys=simpleFormatter

[logger_teja]
level=DEBUG
handlers=fileHandler
qualname=tejaLogger

[handler_fileHandler]
class=logging.FileHandler
level=DEBUG
formatter=simpleFormatter
args=("error.log", "w")

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

我得到以下错误:

^{pr2}$

请帮我解决这个问题。 或者 请解释一下“为什么总是需要包含根记录程序?”在


Tags: 文件debugformatterhandlerskeysloggerlevelloggers
2条回答

如果您use the source,您将看到您必须配置根记录器:

# configure the root first
llist = cp["loggers"]["keys"]
llist = llist.split(",")
llist = list(map(lambda x: x.strip(), llist))
llist.remove("root")
section = cp["logger_root"]
root = logging.root
log = root

(其中cp是加载传入的.ini文件的^{}

我能想到的唯一原因是explicit is better than implicit,所以它迫使您确切地声明您想用根日志记录程序做什么,以防您认为它会带来一些魔力。虽然我觉得这不是一个特别好的理由。这可能正是当时有人想的那样做。如果你做一些further reading

The fileConfig() API is older than the dictConfig() API and does not provide functionality to cover certain aspects of logging [... N]ote that future enhancements to configuration functionality will be added to dictConfig(), so it’s worth considering transitioning to this newer API when it’s convenient to do so.

如果考虑^{} docs,似乎不必提供root记录器。在

因此,您似乎需要来指定根处理程序,除了向后兼容之外,没有什么真正好的理由。如果您想绕过这个问题,您必须在Python文件中指定您的设置,或者导入一个JSON文件并使用dictConfig方法。在

为了以防万一发生在其他人身上,请检查是否有逗号分隔所有记录器条目,因为您可能会缺少一个条目,并合并了两个字段的名称。在

相关问题 更多 >