我正在玩Python Logstash Formatter和 in 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
是要创建新格式化程序的工厂。因此,当日志框架想要创建格式化程序时,它将确保导入logstash_formatter
,然后执行logstash_formatter.LogstashFormatter(*args, **kwargs)
之类的操作.
确实,如果您use the source, Luke ,你可以看到
- The value is extracted
和the created factory is used here
if '()' in config: factory = config['()'] # for use in exception handler
And later the factory is called with kwargs :
result = factory(**kwargs)
关于python - 日志配置字典中的空括号 "()"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38729099/