I'm trying to change the log level of the root logger. It appears to change when queried withgetEffectiveLevel() but doesn't actually log anything different than it did before the change.
For example:
>>> import logging>>> root_logger = logging.getLogger()>>> root_logger.getEffectiveLevel()30>>> root_logger.warning('test')test>>> root_logger.info('test')>>> root_logger.setLevel(logging.DEBUG)>>> root_logger.getEffectiveLevel()10>>> root_logger.warning('test')test>>> root_logger.info('test')>>> root_logger.debug('test')Why is this? Am I missing something else I need to do? It puzzles me that I can set the level and see it withgetEffectiveLevel but that it doesn't change the behavior at all.
I am using the built in logging module (version 0.5.1.2) on Python 3.7.3.
- You likely also need to adjust the handler level, and maybe something else I'm forgetting.Norrius– Norrius2020-07-29 21:20:35 +00:00CommentedJul 29, 2020 at 21:20
1 Answer1
You haven't configured any handlers for the logger (or the logging system at all).
This means that, based onthe code here,logging defaults tothis last-resort logger, which is a stderr logger with theWARNING threshold, and as such, it doesn't handle those debug messages.
5 Comments
setLevel didn't change the level, even thoughgetEffectiveLevel says it has been changed. Do I need to get the default handler fromroot_logger and then change the level of the default handler?root_logger.handlers will yield[]. Thereare no handlers configured, but thelogging module has a last-resort handler (see the linked code!) so critical messages don't get completely lost.logging.basicConfig(level=logging.WARNING) to configure a non-last-resort handler on the root logger.getEffectiveLevel is reporting the set level? It seems like even if I rely on a last-resort handler,setLevel should set the log level like you'd expect.getEffectiveLevel andsetLevel agree, but the last-resort handler appears to not behave accordingly.root_logger.setLevel(logging.ERROR) and emitting a warning; the warning will not be emitted as the logger filters it out. Handlers have their own level filter settings; you might want an email-sending handler to only handle error messages, but be attached to the root logger anyway.Explore related questions
See similar questions with these tags.

