如何在 python 的 YAML 文件中设置 logging.handlers.SysLogHandler 的输出
How to set output for logging.handlers.SysLogHandler in YAML file in python
我有以下配置文件:
[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]
如何以yaml格式为sysHandler提供参数?为什么简单地将 args 放在原始配置文件中就可以了,在这里我必须指定流/文件名?
日志记录 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()
并且不是可用于实例化的实例的关键字class.
StreamHandler (i.e. stream
) and FileHandler(即 filename
)的 non-mandatory/optional 键与各自的签名相匹配。
我假设您真的必须提供带有元组参数的地址作为序列,才能接受:
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)
我有以下配置文件:
[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]
如何以yaml格式为sysHandler提供参数?为什么简单地将 args 放在原始配置文件中就可以了,在这里我必须指定流/文件名?
日志记录 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()
并且不是可用于实例化的实例的关键字class.
StreamHandler (i.e. stream
) and FileHandler(即 filename
)的 non-mandatory/optional 键与各自的签名相匹配。
我假设您真的必须提供带有元组参数的地址作为序列,才能接受:
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)