【问题标题】:How to set output for logging.handlers.SysLogHandler in YAML file in python如何在 python 的 YAML 文件中设置 logging.handlers.SysLogHandler 的输出
【发布时间】:2016-08-30 10:23:31
【问题描述】:

我有以下配置文件:

[loggers]
keys=MYLogger

[handlers]
keys=fileHandler,streamHandler,sysHandler

[formatters]
keys=simpleFormatter

[logger_MYLogger]
level=INFO
handlers=fileHandler
qualname=MYLogger
propagate=0

[handler_fileHandler]
class=FileHandler
formatter=simpleFormatter
args=('mylog.log',)

[handler_streamHandler]
class=StreamHandler
formatter=simpleFormatter
args=(sys.stdout,)

[handler_sysHandler]
class=logging.handlers.SysLogHandler
formatter=simpleFormatter
args=('/dev/log',)


[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s : %(message)s
datefmt=%Y-%m-%d %H:%M:%S

我需要将其转换为 YAML 文件。我已经成功完成了,除了 sysHandler 的一部分:

  version: 1
  formatters:
    simpleFormatter:
      format: '%(asctime)s - %(name)s - %(levelname)s : %(message)s'
      datefmt: '%Y-%m-%d %H:%M:%S'
  handlers:
    stream:
      class: logging.StreamHandler
      formatter: simpleFormatter
      stream: ext://sys.stdout
    file:
      class: logging.FileHandler
      formatter: simpleFormatter
      filename: mylog.log
    # sys:
    #   class: logging.handlers.SysLogHandler
    #   formatter: simpleFormatter
    #   stream: /dev/log
  loggers: 
    MYLogger:
      level: INFO
      handlers: [stream, file]

如何为 sysHandler 提供 yaml 格式的参数?为什么可以简单地将 args 放在原始配置文件中,而在这里我必须指定流/文件名?

【问题讨论】:

    标签: python logging yaml syslog


    【解决方案1】:

    日志记录dictionary schema details 对处理程序有以下说法:

    handlers - the corresponding value will be a dict in which each key is a handler 
    id and each value is a dict describing how to configure the corresponding Handler
    instance.
    
    The configuring dict is searched for the following keys:
    
        class (mandatory). This is the fully qualified name of the handler class.
        level (optional). The level of the handler.
        formatter (optional). The id of the formatter for this handler.
        filters (optional). A list of ids of the filters for this handler.
    
    All other keys are passed through as keyword arguments to the handler’s
    constructor.
    

    SysLogHandler 有以下signature

    SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM)
    

    stream 不是处理程序的强制或可选键,它作为关键字参数传递给 SysLogHandler(),它不是可用于实例化该类实例的关键字。

    StreamHandler(即stream)和FileHandler(即filename)的非强制/可选密钥与各自的签名匹配)。

    我假设您确实必须提供以元组参数作为序列的地址,以使其被接受:

    sys:
        class: logging.handlers.SysLogHandler
        formatter: simpleFormatter
        address: [localhost, /dev/log]
    

    (请注意,INI 样式配置文件中[handler_sysHandler]args 条目应以元组开头。来自logging documentation

    args=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER)
    

    【讨论】:

    • 我自己想出了地址,但感谢您的回复并注意.ini文件中的元组
    猜你喜欢
    • 2015-07-19
    • 2012-12-05
    • 2017-12-21
    • 2020-01-13
    • 2010-10-04
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    相关资源
    最近更新 更多