Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logging.DEBUG level doesn't logging

I have a problem with python's logging lib. With the code below I create a "logger":

logger = logging.getLogger()
def logger_init(level):
    try:
        syslog = SysLogHandler(address=LOG_DESTINATION)
    except Exception, ex:
        return
    formatter = logging.Formatter('%(module)s[%(process)d]: %(message)s')
    syslog.setFormatter(formatter)
    syslog.setLevel(level)
    logger.addHandler(syslog)

And I call it like:

logger.debug(SOME_STR_TO_BE_LOGGED)

OR like:

logger.error(SOME_STR_TO_BE_LOGGED)

And I initialize the logger with:

log_level = logging.ERROR
if options.DEBUG_LOG: ####  This comes from options parser and it is True.
    log_level = logging.DEBUG
logger_init(log_level)

The problem is that the error, and warn is working very well, but neither info nor debug methods prints anything to syslog.

I'm using syslog-ng and I designed my filter, that is it will accept every level from debug to emerg.

What is the problem here? Any ideas?

like image 510
0xmtn Avatar asked Dec 13 '12 10:12

0xmtn


People also ask

What is the default logging level in Python?

The default level is WARNING , which means that only events of this level and above will be tracked, unless the logging package is configured to do otherwise. Events that are tracked can be handled in different ways. The simplest way of handling tracked events is to print them to the console.

How does logging level work Python?

Logging LevelsWhen you set a logging level in Python using the standard module, you're telling the library you want to handle all events from that level on up. If you set the log level to INFO, it will include INFO, WARNING, ERROR, and CRITICAL messages. NOTSET and DEBUG messages will not be included here.

What is the logging function for DEBUG level?

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. Another particularity of the root logger is that its default handler will be created the first time a log with a level greater than WARN is logged.


1 Answers

You also have to set the level of the logger, not only the handler.

Add this to your logger_init:

logger.setLevel(level) 
like image 191
Daniel Hepper Avatar answered Oct 06 '22 06:10

Daniel Hepper