Automatic Logging

D

daftspaniel

Sorry if this is a FAQ but Google returns a *lot* of results for Python
Logging :)

I am looking for a tool that will automatically add logging to existing
code e.g. Function Entries and Exits, Return values etc.

Thanks,
Davy Mitchell

Mood News
- BBC News Headlines Auto-Classified as Good, Bad or Neutral.
http://www.latedecember.com/sites/moodnews/
 
P

Peter Hansen

Sorry if this is a FAQ but Google returns a *lot* of results for Python
Logging :)

I am looking for a tool that will automatically add logging to existing
code e.g. Function Entries and Exits, Return values etc.

Perhaps you are looking for nothing more than this standard function:
Help on built-in function settrace in module sys:

settrace(...)
settrace(function)

Set the global debug tracing function. It will be called on each
function call. See the debugger chapter in the library manual.


Combine that with the standard library "logging" package and it meets
your specs as stated above.

-Peter
 
G

Gregory Petrosyan

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
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top