Using external modules in a logging configuration file

I have set up the following config file for the logging module in python2.7 using 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

      

Unfortunately, this is about as small as I can make the file for this example. I am using a module called logstash

that sends my registration messages to be viewed remotely. If I try to load this config with logging.config.fileConfig

, I get an error when it cannot find logstash. All I have to do is import the logstash before loading and the problem goes away.

It would be nice if I could put this import statement in the config file itself, so the file loader didn't need to know or care. It would also be fine for things like urlparse

, so I wouldn't need so many environment variables.

Is there a way to do this?

+3


source to share


1 answer


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

Short answer

According to the documentation , you can write something like:

class=ext://logstash.LogstashHandler

      

I'm not sure if the logstash needs to be imported into the script so that it doesn't sort the logging configuration file (since logstash is not part of the standard library).

Alternative answer



But I must add that this mechanism hasn't always worked very well for me in old style logs config files ... so if that doesn't work I would recommend turning your config file to yaml one and use this very function:

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

      

For the other arguments, I don't know how to write them, as I can't find any documentation about logstash. If they are positional arguments, this should be OK (although I'm not sure about the usage int

in the logging config file):

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

      

But if they are keyword arguments, it must be written differently:

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

      

0


source







All Articles