Creating logged functions using decorators

  • Thread starter Nathan Harmston
  • Start date
N

Nathan Harmston

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 ).

def log(fn):
def newfn(*args):
print datetime.date.today()
print __file__
print fn.__name__
print args
return fn(*args)
return newfn

@log
def doAnalysis(a, b):
print a, b
return a + b

doAnalysis(3, 7)

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
 
P

Paul McGuire

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 ).

def log(fn):
def newfn(*args):
print datetime.date.today()
print __file__
print fn.__name__
print args
return fn(*args)
return newfn

@log
def doAnalysis(a, b):
print a, b
return a + b

doAnalysis(3, 7)

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
return newfn

Here's another idea: wrap the call to fn inside a try/except block, so
that if the function raises an exception, you can log it and then
reraise it.

-- Paul
 
C

Carsten Haese

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.
 
M

Michele Simionato

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 ).

def log(fn):
def newfn(*args):
print datetime.date.today()
print __file__
print fn.__name__
print args
return fn(*args)
return newfn

@log
def doAnalysis(a, b):
print a, b
return a + b

doAnalysis(3, 7)

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


Using my own decorator module (http://www.phyast.pitt.edu/~micheles/
python/documentation.html)
that would be

from decorator import decorator

@decorator
def log(f, *args, **kw):
result = f(*args, **kw)
print args, kw, result
return result

Michele Simionato
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top