py.log using decorators for DRY

yoda nochiel at gmail.com
Sat Oct 29 13:23:28 EDT 2005


I'm using py.log for logging and I find that I end up having the
following pattern emerge within my code (influenced by
http://agiletesting.blogspot.com/2005/06/keyword-based-logging-with-py-library.html):


def foo(**kwargs):
    log.foo(kwargs)
    #body form

This led me to believe that I could simplify that pattern with the
following idiom :


def logit (fn):
    '''
    decorator to enable logging of all tagged methods
    '''
        def decorator (**kwargs):
        # call a method named fn.func_name on log with kwargs
        #should be something like: log.func_name (kwargs)

    return decorator


I can then do add @logit to all my existing methods via a script
(there's a truck load of methods to tag):


@logit
def oldfoo () : pass


My question is in regards to the body form in the decorator.  How do I
call that method on the log object at runtime?

(ps.  I hope my question is clear $)




More information about the Python-list mailing list