Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring root logger in python

I have the following logging configuration in in my Django settings.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format':
              '%(levelname)s|%(asctime)s|%(name)s>> %(message)s',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        }
    },
    'loggers': {
         '': {
            'handlers': ['console'],
            'level': 'ERROR',
            'propagate': True,
         },
        'apps': {
            'handlers': ['console'],
            'level': 'DEBUG',
        },
    }
}

With this configuration I expect my 'apps' to log at DEBUG level and any other modules to log only ERROR and above. But I see DEBUG messages from other modules. How do I fix it?

like image 785
Chamindu Avatar asked Jan 09 '14 13:01

Chamindu


People also ask

What is root logger in Python?

On top of the hierarchy is the root logger, which can be accessed via logging. root. This logger is called when methods like logging. debug() is used. By default, the root log level is WARN, so every log with lower level (for example via logging.info("info") ) will be ignored.

What is logging config in Python?

The logging configuration functionality tries to offer convenience, and in part this is done by offering the ability to convert text in configuration files into Python objects used in logging configuration - for example, as described in User-defined objects.

Can we use log4j for Python?

log4j is a reliable, fast and flexible logging framework (APIs) written in Java, which is distributed under the Apache Software License. log4j is a popular logging package written in Java. log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.


2 Answers

Are you using an empty string key in LOGGING['loggers'] to match the root logger? If so, you could try this instead.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format':
              '%(levelname)s|%(asctime)s|%(name)s>> %(message)s',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        }
    },
    'loggers': {
        'apps': {
            'handlers': ['console'],
            'level': 'DEBUG',
        }
    },
    'root': {
       'handlers': ['console'],
       'level': 'ERROR'
    }
}
like image 170
Rafa Avatar answered Oct 19 '22 06:10

Rafa


I had a similar issue, with root logger configured a level at INFO but seeing DEBUG log message.

Turns out I should not set 'propagate': True for my other logger which is at level DEBUG, since that those logs will be passed to the root logger no matter what level root at.

So my guess here to the original question is that there might be some other modules' logger with propagate turned on. set disable_existing_loggers to True maybe solve this.

like image 33
PalmChou Avatar answered Oct 19 '22 07:10

PalmChou