将关键字 arg 传递给 python 日志记录配置文件中的日志处理程序
Pass keyword arg to log handler in python logging configuration file
根据 https://docs.python.org/2/library/logging.config.html#configuration-file-format,我可以使用 args
键为记录器的构造函数指定参数。但这似乎只适用于位置参数。我有一个需要一些关键字参数的处理程序。有没有办法在配置文件中指定它们?
很遗憾地告诉你,答案是否。
查看 https://hg.python.org/cpython/file/2.7/Lib/logging/config.py#l163 了解更多详情。
如有疑问,源代码永远是您的朋友!
好吧,我不同意答案是否定的。
你可以把你的关键字参数放在一行中,就像这个例子中的 SysLogHandler,没有位置参数,只有关键字参数:
class logging.handlers.SysLogHandler(address=('localhost',
SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM)
[handler_mainHandler]
class=logging.handlers.SysLogHandler
level=INFO
formatter=defaultFormatter
args=('/dev/log','myapp')
Python 会将“/dev/log”作为地址关键字参数,将 'myapp' 作为设施参数。他们按出现的顺序处理。
的文档中
[handler_hand05]
class=handlers.SysLogHandler
level=ERROR
formatter=form05
args=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER)
虽然有一些缺点:
可读性不是很好
有限制:如果需要访问外部库,使用ext://
效果不佳。在这种情况下,我建议将旧式配置文件转换为 yaml 文件,如我的 question/answer 此处所述:
'Upgrading' 将旧式编写的配置文件转换为 yaml 编写的配置文件,允许以更好、更明确的方式使用关键字参数。请注意,文档以及 python2 and python3 明确讨论了关键字参数:
The callable will be called with the remaining items in the
configuration sub-dictionary as keyword arguments.
使用 YAML(不是 .ini),如果您使用 () 特殊键而不是 class关键字,同级的所有属性都会作为参数传递给构造函数。
例如使用外部格式化程序,这将忽略 log_colors 参数:
formatters:
class:
class: colorlog.ColoredFormatter
log_colors: { DEBUG: 'cyan', INFO: 'green', WARNING: 'yellow', ERROR: 'red', CRITICAL: 'red,bg_white' }
format: '%(log_color)s[%(asctime)s-%(shorter_levelname)s-%(compact_name)s]%(message)s'
datefmt: '%H:%M:%S'
这会起作用:
formatters:
color:
(): colorlog.ColoredFormatter
log_colors: { DEBUG: 'cyan', INFO: 'green', WARNING: 'yellow', ERROR: 'red', CRITICAL: 'red,bg_white' }
format: '%(log_color)s[%(asctime)s-%(shorter_levelname)s-%(compact_name)s]%(message)s'
datefmt: '%H:%M:%S'
根据 https://docs.python.org/2/library/logging.config.html#configuration-file-format,我可以使用 args
键为记录器的构造函数指定参数。但这似乎只适用于位置参数。我有一个需要一些关键字参数的处理程序。有没有办法在配置文件中指定它们?
很遗憾地告诉你,答案是否。
查看 https://hg.python.org/cpython/file/2.7/Lib/logging/config.py#l163 了解更多详情。
如有疑问,源代码永远是您的朋友!
好吧,我不同意答案是否定的。
你可以把你的关键字参数放在一行中,就像这个例子中的 SysLogHandler,没有位置参数,只有关键字参数:
class logging.handlers.SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM)
[handler_mainHandler]
class=logging.handlers.SysLogHandler
level=INFO
formatter=defaultFormatter
args=('/dev/log','myapp')
Python 会将“/dev/log”作为地址关键字参数,将 'myapp' 作为设施参数。他们按出现的顺序处理。
的文档中[handler_hand05]
class=handlers.SysLogHandler
level=ERROR
formatter=form05
args=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER)
虽然有一些缺点:
可读性不是很好
有限制:如果需要访问外部库,使用
ext://
效果不佳。在这种情况下,我建议将旧式配置文件转换为 yaml 文件,如我的 question/answer 此处所述:
'Upgrading' 将旧式编写的配置文件转换为 yaml 编写的配置文件,允许以更好、更明确的方式使用关键字参数。请注意,文档以及 python2 and python3 明确讨论了关键字参数:
The callable will be called with the remaining items in the configuration sub-dictionary as keyword arguments.
使用 YAML(不是 .ini),如果您使用 () 特殊键而不是 class关键字,同级的所有属性都会作为参数传递给构造函数。
例如使用外部格式化程序,这将忽略 log_colors 参数:
formatters:
class:
class: colorlog.ColoredFormatter
log_colors: { DEBUG: 'cyan', INFO: 'green', WARNING: 'yellow', ERROR: 'red', CRITICAL: 'red,bg_white' }
format: '%(log_color)s[%(asctime)s-%(shorter_levelname)s-%(compact_name)s]%(message)s'
datefmt: '%H:%M:%S'
这会起作用:
formatters:
color:
(): colorlog.ColoredFormatter
log_colors: { DEBUG: 'cyan', INFO: 'green', WARNING: 'yellow', ERROR: 'red', CRITICAL: 'red,bg_white' }
format: '%(log_color)s[%(asctime)s-%(shorter_levelname)s-%(compact_name)s]%(message)s'
datefmt: '%H:%M:%S'