After reading the logging documentation of Python and several examples of how to customise the log format and output, I came to the conclusion that formatting can only be set for a single logger instance. But since all libraries in other modules use their own logger instance (so that their source can be identified), they completely ignore how my main application wants to output the log messages.
How can I create a custom logger that formats the records in a specific way (for example with abbreviated levels and corresponding console colours) that is automatically used for all modules when set up in the main function?
What I saw is this:
# main.py:
logger = logging.getLogger('test')
logger.addHandler(ConsoleHandler())
# module.py:
logger = logging.getLogger(__name__)
...
logger.info("a message from the library")
This prints my library log message as before, not with the custom format. It seems rather pointless to apply an output format to a single logger instance if each module has their own one. The formatter must be applied at the application level, not for each library individually. At least that's how I understand and successfully use things around ILogger
in .NET. Can Python do that? I guess what I need is my own ILogger
implementation that is made accessible throughout the application through dependency injection. That's not how Python logging looks like to me.