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

标签 python logging logstash

我正在玩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 是要创建新格式化程序的工厂。因此,当日志框架想要创建格式化程序时,它将确保导入logstash_formatter,然后执行logstash_formatter.LogstashFormatter(*args, **kwargs)之类的操作.

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

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

相关文章:

python - Redshift + SQLAlchemy 长查询挂起

ruby-on-rails - 调试 Rails Controller 进行过多查询

logstash - 如何使用Logstash插件-logstash-input-http

elasticsearch - Logstash Beats Input-多个多行编解码器

elasticsearch - Logstash:从一个 Elasticsearch 迁移到另一个 Elasticsearch 结果会带来一些其他属性

python - 根据 pandas 中的另一列对唯一值进行排序

python - 按组规范化pyspark数据帧

python - 无法使用 Selenium 安装某些 Firefox 扩展

swift - Xcode App仅在设备上崩溃-EXC_CRASH(SIGABRT)

mysql - 两个相似用户的数据库架构