I am sure I am just misunderstanding how this works but I cannot seem to have log formatting propagate to logging in modules unless the module "knows" the name of the module that called it.
The following code demonstrates the issue. The code below works but I have to hard code the name of the module that called it for the formatting to apply.
app.py
import logging import mymodule logger = logging.getLogger('app') log_formatter = logging.Formatter(f'%(asctime)s %(name)s[%(process)d]: %(levelname)s - %(message)s\n') log_handler_console = logging.StreamHandler() log_handler_console.setFormatter(log_formatter) log_handler_console.terminator = '' logger.addHandler(log_handler_console) logger.error('testing 1') mymodule.dosomething() module.py
import logging logger = logging.getLogger('app.module') def dosomething(): logger.error('testing 2') As per code above I get this:
2022-01-09 19:01:53,986 app[46541]: ERROR - testing 1 2022-01-09 19:01:53,987 app.module[46541]: ERROR - testing 2 but if I change the name of the logger to eg 'test' I get:
2022-01-09 19:21:07,590 app[46580]: ERROR - testing 1 testing 2 If I change the name of the logger in line 2 of module.py to anything other than 'app' or 'app.something' then the formatting no longer applies. I get how the inheritance works (I think) but how can I know the calling module name without hard coding it? I did see a few examples here on SO but with cautions that its a bad idea.
Essentially what I am trying to do is define a log format in the main app and have any module that the main app calls use the same format. I am I crazy? Seems like an obvious thing to want?