简体   繁体   English

将关键字 arg 传递给 python 日志记录配置文件中的日志处理程序

[英]Pass keyword arg to log handler in python logging configuration file

According to https://docs.python.org/2/library/logging.config.html#configuration-file-format I can specify arguments to the constructor of a logger, by using the args key.根据https://docs.python.org/2/library/logging.config.html#configuration-file-format我可以使用args键为记录器的构造函数指定参数。 But this seems to work only for positional arguments.但这似乎只适用于位置参数。 I have a handler which needs some keyword arguments.我有一个需要一些关键字参数的处理程序。 Is there a way to specify those in the config file?有没有办法在配置文件中指定那些?

I'm sorry to tell you that the answer is no .我很遗憾地告诉你答案是否定的

Have a look at https://hg.python.org/cpython/file/2.7/Lib/logging/config.py#l163 for more details.有关更多详细信息, 查看https://hg.python.org/cpython/file/2.7/Lib/logging/config.py#l163

When in doubt, the source code is always your friend!如有疑问,源代码永远是您的朋友!

Well I don't agree the answer is just no.好吧,我不同意答案是否定的。

You can put your keyword arguments in a row, like in this example with SysLogHandler, what has no positional arguments, only keyword ones:您可以将关键字参数放在一行中,就像在这个带有 SysLogHandler 的示例中一样,没有位置参数,只有关键字参数:

class logging.handlers.SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM)类 logging.handlers.SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), 设施=LOG_USER, socktype=socket.SOCK_DGRAM)

[handler_mainHandler]
class=logging.handlers.SysLogHandler
level=INFO
formatter=defaultFormatter
args=('/dev/log','myapp')

Python will take '/dev/log' as address keyword argument and 'myapp' as facility argument. Python 将'/dev/log' 作为地址关键字参数,'myapp' 作为工具参数。 They are handled the order they show up.他们按照出现的顺序进行处理。

Such an example is in the documentation for both python2 and python3 :这样的例子在python2python3的文档中:

[handler_hand05]
class=handlers.SysLogHandler
level=ERROR
formatter=form05
args=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER)

There are some disadvantages though:但也有一些缺点:

'Upgrading' the old-style written config file into a yaml written one allows to use keyword arguments in a better and more explicit way.将旧式编写的配置文件“升级”为 yaml 编写的文件,允许以更好、更明确的方式使用关键字参数。 Note that the documentation, as well for python2 and python3 talks explicitely about keyword arguments:请注意,文档以及python2python3明确地讨论了关键字参数:

The callable will be called with the remaining items in the configuration sub-dictionary as keyword arguments. callable 将使用配置子字典中的其余项目作为关键字参数进行调用。

Using YAML (not .ini ), if you use the () special key instead of the class keyword, all the properties at the same level will be passed as arguments to the constructor.使用 YAML(不是.ini ),如果您使用()特殊键而不是class关键字,则同一级别的所有属性都将作为参数传递给构造函数。

Eg using an external formatter, this will ignore the log_colors parameter:例如,使用外部格式化程序,这将忽略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'

This instead will work:这将起作用:

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM