在日志记录配置文件中使用外部模块 [英] Use of external modules in a logging configuration file

查看:88
本文介绍了在日志记录配置文件中使用外部模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用logstash在python2.7中为日志记录模块设置了以下配置文件

I have setup the following configuration file for the logging module in python2.7 with logstash

[loggers]
keys=root,test

[handlers]
keys=remote

[formatters]
keys=standard

[logger_root]
level=NOTSET
handlers=remote

[logger_test]
level=DEBUG
handlers=remote
propagate=0
qualname=test

[handler_remote]
class=logstash.LogstashHandler
level=NOTSET
formatter=standard
args=(os.environ['hostname'], int(os.environ['port']))

[formatter_standard] 
format=%(levelname)s - %(message)s
datefmt=
class=logging.Formatter

不幸的是,这大约与我可以为该示例制作的文件一样短.我使用一个名为logstash的模块,该模块将我的日志消息发送出去以供远程查看.如果尝试使用logging.config.fileConfig加载此配置,则会收到错误消息,提示找不到logstash.我要做的就是在加载之前导入logstash,问题就消失了.

Unfortunately, this is about as short as I can make the file for this example. I use a module called logstash which sends my logging messages out to be viewed remotely. If I try and load this configuration with logging.config.fileConfig I get an error where it cannot find logstash. All I have to do is import logstash before loading and the problem goes away.

不过,如果我可以将该import语句放入配置文件本身,那就太好了,这样文件的加载程序就不必知道或关心了.对于urlparse之类的东西也很好,因此我不需要那么多的环境变量.

It would be nice if I could fit that import statement into the configuration file itself though, so that the loader of the file would not need to know or care. It would also be nice for things like urlparse so I wouldn't need as many environment variables.

有没有办法做到这一点?

Is there a way to do this?

推荐答案

这是一个很老的问题,但是如果有人还在寻找答案,我会发布这个问题(适用于python2和python3).

Well this is quite an old question, but if anyone is still looking for an answer, I post this (available for both python2 and python3).

根据文档 ,您可以编写如下内容:

According to the documentation, you can write something like:

class=ext://logstash.LogstashHandler

我不确定是否有必要在解析日志记录配置文件的脚本中导入logstash(因为logstash不属于标准库).

I'm not sure whether it's necessary or not to import logstash in the script parsing the logging configuration file (as logstash does not belong to the standard library).

但是我必须补充一点,在旧式日志记录配置文件中,这种机制对我而言并不总是很好...所以,如果这种方法行不通,我建议将您的配置文件转换为yaml文件,然后在其中使用此非常相同的功能:

But I must add that this mechanism hasn't always worked very well for me in old-style logging configuration files... so if this doesn't work, I would recommend to turn your configuration file into a yaml one and use this very same feature in it:

handlers:
    remote:
        class: ext://logstash.LogstashHandler
        level: NOTSET
        formatter: standard

对于其他args,我不知道如何编写它们,因为我找不到有关logstash的文档.如果它们是位置参数,则应该可以(尽管我不确定在记录配置文件中使用int):

For the other args, I don't know how to write them since I can't find a documentation about logstash. If they are positional arguments, this should be OK (though I'm not sure about the use of int in a logging configuration file):

        args: [os.environ['hostname'], int(os.environ['port'])]

但是,如果它们是关键字参数,则应该用另一种方式编写:

But if they are keyword arguments, this should be written another way:

        kw1: os.environ['hostname']
        kw2: int(os.environ['port'])

这篇关于在日志记录配置文件中使用外部模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆