2

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.

askedJul 29, 2020 at 21:16
Salvatore's user avatar
1
  • You likely also need to adjust the handler level, and maybe something else I'm forgetting.CommentedJul 29, 2020 at 21:20

1 Answer1

0

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.

answeredJul 29, 2020 at 21:20
AKX's user avatar
Sign up to request clarification or add additional context in comments.

5 Comments

I understand how it resolved the default logging handler. What confuses me is whysetLevel 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?
With your example,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.
Use e.g.logging.basicConfig(level=logging.WARNING) to configure a non-last-resort handler on the root logger.
Do you know whygetEffectiveLevel 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.
Thelogger itself is configured to that level -- try setting it to a higher level, e.g.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.

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.