I would like to create a decorator adding a logger to any decorated class. I succeeded with:
def logged(cls_to_decorate):
    log_name = cls_to_decorate.__module__
    logger = logging.getLogger(log_name)
    setattr(cls_to_decorate, 'logger', logger)
    return cls_to_decorate
Its usage:
@logged
class TestFoo(TestCase):
    def test_foo_function(self):
         self.logger.debug("ciao!!")
Now let's suppose I want to pass to this decorator an additional parameter, so to use it as follows:
@logged(logging_level=logging.DEBUG)
class TestFoo(TestCase):
    pass
I tried to use the syntax of the decorators for functions/methods (with wrapped function), but of course since we're talking about a class as parameter it doesn't work.
The decorator should be something like this:
...
def logged(cls_to_decorate, logging_level=None):
    logging.basicConfig(level=logging_level)
    log_name = cls_to_decorate.__module__
    logger = logging.getLogger(log_name)
    setattr(cls_to_decorate, 'logger', logger)
    return cls_to_decorate
...
NOTE: the object to be decorated is a class and not a function.
Solution python decorators with parameters should apply, but I tried the following:
def logged(logging_level=None):
    def class_decorator(cls_to_decorate):
        # Do something with arguments
        logging.basicConfig(level=logging_level)
        log_name = cls_to_decorate.__module__
        logger = logging.getLogger(log_name)
        setattr(cls_to_decorate, 'logger', logger)
        return cls_to_decorate
    return class_decorator
But I have an error every time I wrap a class takling some argument:
E               TypeError: class_decorator() takes exactly 1 argument (2 given)
Thanks for your help!
 
     
    