Is this a DRY solution?
class DeviceLoggerAdapter(logging.LoggerAdapter):
def process(self, msg, kwargs):
kwargs["extra"] = kwargs.get("extra", {})
if hasattr(self, "instance"):
kwargs["extra"]["device_name"] = self.instance.name
return msg, kwargs
class MyClass:
def __init__( name: str = ""):
self.name = name
self.logger_adapter = DeviceLoggerAdapter(logger, {"device_name": name})
self.logger_adapter.instance = self # Riferimento all'istanza
def method(self):
logger.info(
f"{self.logger_adapter.extra['device_name']}: my text from this method"
)
Thank you very much AI, but more thanks to the one that keep pressing minus on the question without any explanation