Question

I don't know why it can't log that message, i think everything is correctly set.

And logging.DEBUG is defined under logging module

import logging
import sys

logger = logging.getLogger('collega_GUI')
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s %(levelname)s --file: %(module)s --riga: %(lineno)d, %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

logger.debug('def __init__')

But if i try to run this one, it works:

logger.warning('def __init__')

Where is the problem with this level variable?

Was it helpful?

Solution

The problem is that the debug level message was filtered out by the logger before it ever got to the handler. The problem is fixed by changing handler.setLevel(logging.DEBUG) to logger.setLevel(logging.DEBUG).

You can filter by log level in several different places as a log message is passed down the chain. By default, loggers only pass INFO and above and handlers accept everything. Allowing handlers to use different log levels is useful if you want different levels of logging to go to different places. For example, you could set your logger to DEBUG and then create one handler that logs to the screen at WARN and above, and another handler that logs to a file at DEBUG and above. The user gets a little info and the log file is chatty.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top