gpt4 book ai didi

python - 日志配置字典中的空括号 "()"是什么意思?

转载 作者:行者123 更新时间:2023-11-28 22:34:49 27 4
gpt4 key购买 nike

我正在玩弄 Python Logstash Formatterin its wiki它建议为格式化程序设置以下选项:

"formatters": { "logstash":{ "()": "logstash_formatter.LogstashFormatter" } }

这对我有用,但我不确定空括号的用途,或者在此示例中 logstash_formatter.LogstashFormatter 的确切设置。

有人可以向我解释这里空括号与 Python 记录器相关的含义吗?它几乎看起来像是一个空元组,但我无法理解如何将一个空元组设置为一个类。

最佳答案

如果您查看 python docs for logging ,你会看到这个:

Objects to be configured are described by dictionaries which detail their configuration. In some places, the logging system will be able to infer from the context how an object is to be instantiated, but when a user-defined object is to be instantiated, the system will not know how to do this. In order to provide complete flexibility for user-defined object instantiation, the user needs to provide a ‘factory’ - a callable which is called with a configuration dictionary and which returns the instantiated object. This is signalled by an absolute import path to the factory being made available under the special key '()'.

基本上它的意思是 logstash_formatter.LogstashFormatter 是将要创建新格式化程序的工厂。因此,当日志框架想要创建格式化程序时,它会确保 import logstash_formatter 然后执行类似 logstash_formatter.LogstashFormatter(*args, **kwargs) 的操作.

的确,如果你use the source, Luke ,你可以看到

关于python - 日志配置字典中的空括号 "()"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38729099/

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