I would like to inject a custom logger instance into a use case function:
def api_func(project_dir: str, case_name: str) -> None:
"""This function will be called from web API by multiple clients."""
logger = get_logger(project_dir, case_name)
some_use_case(logger)
This is my get_logger
function:
import logging
import os
def get_logger(project_dir: str, case_name: str) -> logging.Logger:
logger = logging.getLogger(case_name) # <- singleton...
logger.setLevel(logging.INFO)
fh = logging.FileHandler(
os.path.join(project_dir, 'file.log'),
delay=True,
)
logger.addHandler(fh)
return logger
The issue is that multiple clients call the same function api_func
with the case_name
, that can be occasionally the same. As a result the same logger instance will be picked up by multiple clients, leading to a shared log data, which is undesirable.
Can I instantiate loggers under the same name, but that can actually be different objects?
The documentation https://docs.python.org/3/library/logging.html#logger-objects explicitly mentions to never instantiate logger instances directly, and instead suggests the use of logging.getLogger
function. But this returns a reference to the very same object, which I would like to avoid. What I need is to create a brand new logger instance every time I call get_logger
function. Is it possible?
Are there any issues associated with instantiating a logger directly with logging.Logger
? The documentation does not seem to mention any of them.
Logger names should relate to an area of the application (eg a Python module, or part of one) and never related to application data such as a case name. You are not using logging the way it is meant to be used, so it's not surprising that you're running into problems. Seethis blog post for a high-level view on logging abstractions (too long to reproduce here).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.