0

Running this

import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

logger.warning("warning")
logger.info("info")
logger.debug("debug")

only prints "warning". I expect it to print all three messages. What am I doing wrong?

I know this should be the most basic thing in the world, but it's got me stumped, and the Python documentation is confusing on this point.

W.P. McNeill
  • 16,336
  • 12
  • 75
  • 111

2 Answers2

1

You never called logging.basicConfig() (or something similar) to establish any handlers. As a result, the call to logger.warning calls it for you, which resets the logging level to logging.WARNING. (Technically, the documentation only mentions the module-level functions logging.warning et al. calling logging.basicConfig for you, so I don't know if this explanation is entirely correct. Calling basicConfig does, however, solve the problem.)

import logging

logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

logger.warning("warning")
logger.info("info")
logger.debug("debug")
chepner
  • 497,756
  • 71
  • 530
  • 681
  • The documentation is confusing because it leaps into the details of logger customization without putting the bare minimum module-level example up front. As a result I got fuzzy on the difference between "logging" and "logger". – W.P. McNeill Jan 25 '20 at 19:21
  • The module [creates an instance of `Logging`](https://github.com/python/cpython/blob/3.8/Lib/logging/__init__.py#L1888) to serve as the root logger. `logging.getLogger` returns an instance that "inherits" from this root logger, and the module-level functions `logging.warning` et al. are just [wrappers](https://github.com/python/cpython/blob/3.8/Lib/logging/__init__.py#L2047) around the root logger calling its `warning` method. – chepner Jan 25 '20 at 19:27
  • But even knowing that, it's not obvious to me what happens when you call the method without having first created any handlers. – chepner Jan 25 '20 at 19:32
1

I figured this out almost immediately after asking.

import logging

logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)

logging.warning("warning")
logging.info("info")
logging.debug("debug")

The documentation is confusing because it leaps into the details of logger customization without putting the bare minimum module-level example up front. Hopefully, this post can help correct that.

This is essentially the same question as Set logging levels, but I'm leaving my answer up because it is slightly more minimal than the one there.

W.P. McNeill
  • 16,336
  • 12
  • 75
  • 111
  • "The documentation is confusing because it leaps into the details of logger customization"? `basicConfig()` is in literally the second code snippet in the HOWTO documentation: https://docs.python.org/3/howto/logging.html#logging-to-a-file and the third para in the reference docs says "The module provides a lot of functionality and flexibility. If you are unfamiliar with logging, the best way to get to grips with it is to see the tutorials (see the links on the right)." with links to the HOWTO tutorials. – Vinay Sajip Jan 27 '20 at 10:53