I want my function do_x to use (child) logger __main__.do_all that is defined in the parent scope do_all from which do_x is called. Instead the root logger is used by default.
Is there a way do force do_x to use the child logger __main__.do_all? Should I pass the logger as parameter to do_x, even though it is not recommended in numerous posts? Assume that I want to call do_x also from other scopes using other (child) loggers.
import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(funcName)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def do_x():
logger.warning('doing x')
def do_all():
logger = logging.getLogger(__name__ + '.do_all')
logger.warning('Starting do_all')
do_x()
def do_all_other():
logger = logging.getLogger(__name__ + '.do_all_other')
logger.warning('Starting do_all_other')
do_x()
do_all()
do_all_other()
Produces
2022-09-27 11:34:40,562 - __main__.do_all - do_all - WARNING - Starting do_all
2022-09-27 11:34:40,562 - __main__ - do_x - WARNING - doing x
2022-09-27 11:34:40,562 - __main__.do_all_other - do_all_other - WARNING - Starting do_all_other
2022-09-27 11:34:40,562 - __main__ - do_x - WARNING - doing x
I want the second and the last line of the output to use __main__.do_all and __main__.do_all_other respectively
do_x()will need to know where it is called from. There is only two ways: pass the name of the parent as a param, or inspect the stackgetLoggerreturns (creating if necessary) a specific logger instance that is managed by theloggingmodule, regardless of where the call togetLoggeris made.