1

I have a module, that is a wrapper class for C programs.

Each method has various inputs, but the outputs are typically the same, a file name.

class Wrapper(object): 
    def wrapper1(self, infile, outfile):
        do_stuff

        log.info('wrapper1:%s' %outfile)

I've already written 50 methods before I realized that I want this behaviour.

Plus, there's got to be an elegant way of adding a logger w/o adding that line to all methods.

Cheers

1 Answer 1

1

Use a decorator:

import functools
def log(func):
    @functools.wraps(func)
    def wrapper(self, infile, outfile):
        retval = func(self, infile, outfile)
        log.info('wrapper1:%s' %outfile)
        return retval
    return wrapper

class Wrapper(object):
    @log
    def wrapper1(self, infile, outfile):
        do_stuff
Sign up to request clarification or add additional context in comments.

3 Comments

Don't forget to forward infile/outfile to func
I'll have to study that answer, thanks ! And there should be a way to get the name of the method automagically as well correct ?
ok...I think it got to work...don't understand all the bit just yet...wrapper.__name__ will get me the method name I'm looking for.Let me implement it and then I'll accept...Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.