79710678

Date: 2025-07-22 15:02:13
Score: 0.5
Natty:
Report link

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

Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Blacklisted phrase (0.5): thanks
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Self-answer (0.5):
  • Starts with a question (0.5): Is this a
  • High reputation (-1):
Posted by: user2239318