0

I am using this code:

import time
import logging
import os
root = logging.getLogger()
root.setLevel(os.environ.get("LOGLEVEL", "DEBUG"))

def timed(func):
    """This decorator prints the execution time for the decorated function."""
    def wrapper_class():
        def wrapper(*args, **kwargs):
            start = time.time()
            result = func(*args, **kwargs)
            end = time.time()
            logging.debug("{} ran in {}s".format(func.__qualname__, round(end - start, 2)))
            return result
        return wrapper
    return wrapper_class()

class AClass:
    @timed
    def __call__(self, *args):
        return self.run(*args)

class BClass(AClass):
    def run(self, *args):
        # Does things
        return "Done"
BClass().__call__()

When logging the time, I was expecting something like

DEBUG:root:BClass.__call__ ran in 26.22s

But instead I got:

DEBUG:root:AClass.__call__ ran in 26.22s

I have multiple classes that inherit from AClass, and I would like to know which one is running. How can I modify the wrapper to show the name of the inherited class?

0 Answers0