Search code examples
pythonloggingconfigpython-logging

Managing loggers with Python logging


I'm writing a server app which should be able to log at different levels both on the console and a log file.

The problem is, if logging.basicConfig() is set it will log to the console but it must be set in the main thread.

It can also be set with logging.basicConfig(filename='logger.log') to write to a file.

Setting a handle either for console logging (logging.StreamHandler()) or file logging (logging.FileHandler()) complements the logging.baseconfig() option set.

The problem is, that the settings are not independent. What I mean is, the loglevel of logging.baseConfig() must include the Handler level, or it wont be logged.

So if I set the baseConfig to log to file, and a StreamHandler to log to console, the file loglevel must be lower than the console level. (Also, the basicConfig option logs all other logs.)

I tried to create two Handles, one for the console and one for the log file, they work, but whatever log type is specified by basicConfig() will still be displayed duplicating messages.

Is there a way to disable the output of basicConfig()? Or any other way to implement these options?


Solution

  • You don't say in your question exactly what levels you want on your console and file logging. However, you don't need to call basicConfig(), as it's only a convenience function. You can do e.g. (code just typed in, not tested):

    import logging
    
    logger = logging.getLogger(__name__)
    configured = False
    
    def configure_logging():
        global configured
        if not configured:
            logger.setLevel(logging.DEBUG) # or whatever
            console = logging.StreamHandler()
            file = logging.FileHandler('/path/to/file')
            #set a level on the handlers if you want;
            #if you do, they will only output events that are >= that level
            logger.addHandler(console)
            logger.addHandler(file)
            configured = True
    

    Events are passed to the logger first, and if an event is to be processed (due to comparing the level of the logger and the event) then the event is passed to each handler of the logger and all its ancestors' handlers as well. If a level is set on a handler the event may be dropped by that handler, otherwise it will output the event.