[英]JSON-log-formatter with logging.config.dictConfig
Recently discovered JSON-log-formatter and would like to use it to write my log output in JSON.最近发现了JSON-log-formatter并想用它来用 JSON 编写我的日志输出。 Configuring it in the script is relatively straight forward, but most of my existing code utilizes logging.config.dictConfig to load the logging config from a YAML file so I can easily configure logging without modifying the script itself, and I can't figure out how to add a python module as a formatter within the config.
在脚本中配置它相对简单,但我现有的大部分代码利用 logging.config.dictConfig 从 YAML 文件加载日志配置,这样我就可以轻松配置日志而无需修改脚本本身,我想不通如何在配置中添加一个 python 模块作为格式化程序。
Current YAML当前的 YAML
version: 1
disable_existing_loggers: False
formatters:
simple:
format: "%(asctime)s | %(name)s | %(funcName)s() | %(levelname)s | %(message)s"
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
file_handler:
class: logging.handlers.TimedRotatingFileHandler
level: INFO
formatter: simple
filename: LogFile.log
encoding: utf8
backupCount: 30
encoding: utf8
when: 'midnight'
interval: 1
delay: True
loggers:
my_module:
level: ERROR
handlers: [console]
propagate: no
root:
level: INFO
handlers: [console, file_handler]
The above config works, but I can't figure out how to change the formatter to the JSON using that module.上面的配置有效,但我不知道如何使用该模块将格式化程序更改为 JSON。 Something like this(I know this doesn't work):
像这样(我知道这行不通):
formatters:
simple:
format: json_log_formatter.VerboseJSONFormatter()
Python loading up that YAML Python 加载那个 YAML
import logging.config
import json_log_formatter
import yaml
with open('./logging.yaml', 'r') as stream:
logConfig = yaml.load(stream, Loader=yaml.FullLoader)
logging.config.dictConfig(logConfig)
I get that python is just loading the YAML into a dictionary object and then configuring logging based on that dictionary, but how do I set the formatter so that it correctly references that JSON logging module and uses it?我知道 python 只是将 YAML 加载到字典对象中,然后根据该字典配置日志记录,但是如何设置格式化程序以使其正确引用该 JSON 日志记录模块并使用它?
nevermind, of course I figured it out after posting this.没关系,当然我在发布这个之后就明白了。
formatters:
myformat:
(): json_log_formatter.VerboseJSONFormatter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.