Reputation: 3447
I'm trying to use the logging facility of python, but it does not work as expected.
Below is my code. It prints warning
and critical
messages properly, but it does not print info
and debug
. getEffectiveLevel()
shows the correct level.
import logging
logger = logging.getLogger("base")
# set level to debug
logger1.setLevel(logging.DEBUG)
# check, if level is really DEBUG
print("effective level: " + str(logger1.getEffectiveLevel()))
logger1.debug ("logger1: debug msg")
logger1.info ("logger1: info msg")
logger1.warning ("logger1: warning msg")
logger1.critical("logger1: critical msg")
Upvotes: 1
Views: 221
Reputation: 3447
The missing information is a little bit hidden in the docs. You need to call
logging.basicConfig()
To initialize the handlers
.
The full code would look like this:
import logging
logger = logging.getLogger("base")
# setup handlers
logging.basicConfig()
# set level to debug
logger1.setLevel(logging.DEBUG)
# check, if level is really DEBUG
print("efffective level: " + str(logger1.getEffectiveLevel()))
logger1.debug ("logger1: debug msg")
logger1.info ("logger1: info msg")
logger1.warning ("logger1: warning msg")
logger1.critical("logger1: critical msg")
Also see how the printed messages changed.
Reference: https://docs.python.org/3/library/logging.html , search for logging.log(level, msg, *args, **kwargs)
and read the Note
Upvotes: 2