debugging - Python : Automatic debug logs when control goes inside/outside function -


i trying 1 project, has many functions. using standard logging module requirement log debug logs says

<timestamp> debug entered foo()  <timestamp> debug exited foo() <timestamp> debug entered bar() <timestamp> debug exited bar() 

but dont want write debug logs inside every function. there way in python takes care of automatic log containing entry , exit of functions?
dont want use decorator functions, unless solution in python.

thanks in advance

any reason don't want use decorator? it's pretty simple:

from functools import wraps import logging  logging.basicconfig(filename='some_logfile.log', level=logging.debug)  def tracelog(func):      @wraps(func) # preserve docstring     def inner(*args, **kwargs):         logging.debug('entered {0}, called args={1}, kwargs={2}'.format(func.func_name, *args, **kwargs))         func(*args, **kwargs)         logging.debug('exited {0}'.format(func.func_name))      return inner 

if that, passing in independent logger layer deep:

def tracelog(log):      def real_decorator(func):         @wraps(func)         def inner(*args, **kwargs):             log.debug('entered {0} called args={1}, kwargs={2}'.format(func.func_name, *args, **kwargs))             func(*args, **kwargs)             log.debug('exited {0}'.format(func.func_name))         return inner     return real_decorator 

cool thing, works functions , methods

usage example:

@tracelog(logger) def somefunc():     print('running somefunc') 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -