I've been going around in circles with this for the last four hours. I've read the docs and this other SO page. This GitHub code recommends importing modules after logging has been setup in the main script, whereas this post suggests each module should have a complete implementation of the logger.
The way logging works has me completely mystified. I'm running Python 3.9.
Here the code at the top of my main script:
import logging
def logger():
# Adding the following line logs each message twice to the console
# logging.basicConfig(level=logging.DEBUG)
# Create the logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Create handlers
ch = logging.StreamHandler()
fh = logging.FileHandler('log.log', mode='w')
ch.setLevel(logging.DEBUG)
fh.setLevel(logging.DEBUG)
# Create formatters
ch.setFormatter(logging.Formatter('[ %(levelname)s ] %(name)s : %(message)s'))
fh.setFormatter(logging.Formatter('%(asctime)s [ %(levelname)s ] %(name)s : %(message)s'))
# Add handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)
return logger
logger = logger()
import log_module # Linter complains but at this point I don't care anymore
logger.debug('Help')
log_module.foo()
In log_module.py
, I have the following:
import logging
logger = logging.getLogger(__name__)
def foo():
logger.setLevel(logging.DEBUG)
logger.debug('Debugging in module')
If I run the main script, I only get one log message: [ DEBUG ] __main__ : Help
.
If I uncomment the line with basicConfig
in the logger
function, I get three log messages, unformatted:
[ DEBUG ] __main__ : Help
DEBUG:__main__:Help
DEBUG:log_module:Debugging in module
If I copy the whole logger()
function over to log_module.py
, I get the output I'm expecting, in the correct format, which is:
[ DEBUG ] __main__ : Help
[ DEBUG ] log_module : Debugging in module
However, it seems very silly to copy entire chunks of code across modules.
- What am I doing wrong?
- What should I be doing instead?
- Does this need to be this convoluted just to get logs from multiple modules?
Thanks a lot in advance for your help.