Python's logging.basicConfig
does not behave as I would expect. In the code below messages sent with logging.warn
get sent even though I've called basicConfig
with a level of ERROR
.
import logging
logging.basicConfig(level=logging.ERROR)
root = logging.getLogger()
print("root.level", logging.getLevelName(root.level))
print("Levels for root.handlers: {}".format(
[logging.getLevelName(handler.level) for handler in root.handlers]))
logging.warn("You're seeing this even though you called basicConfig with level=logging.ERROR")
root.warn("You're seeing this too")
root.setLevel(logging.ERROR)
logging.warn("But you won't see this")
The output produced is
root.level WARNING
Levels for root.handlers: ['NOTSET']
WARNING:root:You're seeing this even though you called basicConfig with level=logging.ERROR
WARNING:root:You're seeing this too
Note that the final call to logging.warn
, once I've called root.setLevel
myself, does indeed fail to produce output as expected.
What good is the level
argument for logging.basicConfig
if it has no effect in this simple setting? And if I want to control what level of messages get output in a simple setting such as this one, is the preferred approach to call root.setLevel
?
Here's my version info:
# sys.version = 3.4.3 |Continuum Analytics, Inc.| (default, Mar 6 2015, 12:03:53) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
There are several questions already on stackoverflow that seem to be the same as this one, but all the ones I've found are actually about other things:
- This guy is trying to run
logging.basicConfig
after he's already done some logging - This question attempts to call
logging.basicConfig
more than once (producing an effect similar to #1) - This question involves using a child logger