Automatic Logging
Gregory Petrosyan
gregory.petrosyan at gmail.com
Fri Jan 27 10:13:59 EST 2006
My decorator bike:
import logging
import traceback
logging.basicConfig(level=logging.DEBUG,
format="%(asctime)s %(levelname)s:\n%(message)s\n",
filename="/tmp/py.log",
filemode='w')
def log(f):
def new_f(*args, **kwds):
try:
indent = len("(in '%s') %s(" % (f.__module__,
f.func_name))
nice_args = [repr(a) for a in args]
nice_args = (',\n'+' '*indent).join(nice_args)
nice_kwds = [str(a) + ' = ' + repr(b) for a,b in
kwds.items()]
if nice_kwds:
nice_kwds[0] = ' '*indent + nice_kwds[0]
nice_kwds = (',\n'+' '*indent).join(nice_kwds)
nice_args = nice_args + ',\n' + nice_kwds
else:
nice_args = nice_args + ', {}'
logging.info("(in '%s') %s(%s) starts" % \
(f.__module__, f.func_name, nice_args))
result = f(*args, **kwds)
except:
logging.error('\n'.join(traceback.format_exc().split('\n')[1:])[:-1])
raise
return result
new_f.func_name = f.func_name
return new_f
More information about the Python-list
mailing list