Creating logged functions using decorators

Carsten Haese carsten at uniqsys.com
Sat Jul 7 15:34:09 EDT 2007


On Sat, 2007-07-07 at 11:59 -0700, Paul McGuire wrote:
> On Jul 7, 1:47 pm, "Nathan Harmston" <ratchetg... at googlemail.com>
> wrote:
> > Hi,
> >
> > I m thinking about writing some code which logs the input and output
> > of a function/script and stores it in a database using sqlalchemy
> > (although I havent started on this yet). I want to do this via a
> > decorator ( I think this is the best way ).
> > [...]
> > I can access the arguments passed to the "logged" function, but is
> > there anyway I can capture the output of the function i.e. 10.
> >
> > Many Thanks in advance,
> >
> > Nathan
> 
> Yes.  Capture the output of the function to a variable and log it
> before you return it.
> 
> def log(fn):
>     def newfn(*args):
>         print datetime.date.today()
>         print __file__
>         print fn.__name__
>         print args
>         returnValue = fn(*args)
>         print returnValue
>         return

What Paul means here is, of course, "return returnValue", not a bare
return.

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list