I have some python async code that emits log lines to document its progress. However, multiple instances of the same code might be running at the same time, so log lines are also intermixed. I would like to assign a unique identifier (a UUID, whatever) to each running task/instance that I can prepend to its log lines, so that logs can later be postprocessed to separate the various logs.
The code also calls functions in other modules, so the usual idiom for module logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
at the beginning of the module doesn’t work for this case. I have access to the code of all modules, so I can make the needed changes.
The question is: how do I configure logging so that each task gets a unique id, writes it to each of its log lines and propagates it to called modules so they can add it to their log lines?