简体   繁体   English

带有 logging.config.dictConfig 的 JSON 日志格式器

[英]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.

相关问题 logging.config.dictConfig似乎不起作用 - logging.config.dictConfig does not seem to work logging.config.dictConfig 的完整示例在哪里? - Where is a complete example of logging.config.dictConfig? 配置日志记录在python中使用logging.config.dictConfig时出错 - Error when config logging use logging.config.dictConfig in python Python 使用 logging.config.dictConfig 以 {} 格式记录 - Python logging with {} format using logging.config.dictConfig 在日志文件名中包含日期并创建特定大小的日志文件,同时使用 `logging.config.dictConfig()` 在 python 中保留最后 3 个日志文件 - Including date in the log file name & creating log files of certain size while keeping last 3 log files in python with `logging.config.dictConfig()` 如果我调用logging.config.dictConfig,记录器将在初始化后发生更改 - logger is changing after init if I call logging.config.dictConfig 如何通过 logging.config.dictConfig - FileHandler 指定 namer & rotator - How to specify namer & rotator through logging.config.dictConfig - FileHandler Python的logging.config.dictConfig()是否应用记录器的配置设置? - Does Python's logging.config.dictConfig() apply the logger's configuration settings? 哪个模块应该包含logging.config.dictConfig(my_dictionary)? my_dictionary怎么样? - Which module should contain logging.config.dictConfig(my_dictionary)? What about my_dictionary? 自定义 Python 日志格式化程序适用于 fileConfig 但不适用于 dictConfig? - Custom Python logging formatter works with fileConfig but not with dictConfig?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM