So I’m running the following code from the command line python:
import logging
rootLog = logging.getLogger(__name__)
rootLog.setLevel(logging.INFO)
rootLog.warning("This is a root warning")
rootLog.info("This is root info")
def info():
log = rootLog.getChild("info")
log.info("This is info")
log.warning("This is a warning")
info()
I’m expecting to see all four log messages on the console, but I’m only seeing the warnings. What is going on? Am I misunderstanding something?
EDIT:
I discovered by adding logging.basicConfig() at the beginning of the script that I’ll get the output that I expected. This is strange, because the python documentation on logging states:
The functions
debug(),info(),warning(),error()andcritical()will callbasicConfig()automatically if no handlers are defined for the root logger.
The bit you quote from the manual is under Module-Level Functions and applies only if the module function
is literally called. Since you are calling an instance method with
rootLog.info()basicConfig isn’t getting called for you and you are probably talking to a null logger. The documentation is kinda confusing there.Use
loggging.basicConfig()and things should work.