0

I have imported Logger class and I have many calls to Logger.error(msg, *args, **kwargs) or Logger.info(msg, *args, **kwargs) in my code.

I want to make those functions (each of the Logger.error/info/warning...) do something else (such as call another function), without changing the calls themeselvs (hundreds of calls).

I that class was something I maintain, I would do something like this:

Change this

class Logger:
    def write(...):

into this:

class Logger:
    def write(...):
        #rename original write to _write and call it from here
        self._write()
        # call new function

and in this way, each call to Logger.write() would keep its old API, but will have an added functionality I desire.

How can I do something like this with a class I can't control?

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • You can use logging librarly. https://docs.python.org/3/library/logging.html – Ali.Turkkan Dec 20 '18 at 08:04
  • Do you realise that you can just change members of classes? The `logging` module is pure Python, there are no true, immutable `builtin` types like `object`. – MisterMiyagi Dec 20 '18 at 08:05
  • @MisterMiyagi Do you mean I can edit the `logging` class? – CIsForCookies Dec 20 '18 at 08:08
  • 1
    You can inherit from it, as in `class MyLogger(logging.Logger):`, or change it, as in `logging.Logger.write = my_log_method`. The later will affect the entire application, so beware. – MisterMiyagi Dec 20 '18 at 08:09

0 Answers0