The question of how to define a logger in one location was asked here: Using Python logging in multiple modules
However the two top answers use basic config or a file config. I don't want to use a file config because I want to be able to dynamically adjust the level with command line arguments. I don't want to use a basic config because, well, my config won't be basic.
This is not library code so I'm not concerned with whatever might happen if the module is imported. My code also has a single entry point.
This is (a simplified) version of what I have at the moment:
temp1.py :
import logging
import sys
import temp2
formatter = logging.Formatter(
fmt = '%(asctime)s - %(module)s - %(levelname)s ===> %(message)s'
)
logger = logging.getLogger('root')
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info("THIS IS AN INFO MESSAGE IN TEMP 1")
temp2.do_something()
temp2.py:
import logging
logger = logging.getLogger(__name__)
logger.info('In temp 2')
def do_something():
logger.critical('doing someting in temp 2')
This is the output:
2018-11-23 18:31:40,543 - temp1 - INFO ===> THIS IS AN INFO MESSAGE IN TEMP 1
doing something in temp 2
The output I desire would: 1) Format the output in the file temp2.py as well and 2) Also log the module_level logging message in temp2.py
I've been through the docs but I can't figure this one out. What do I need to do?