logging - How can I log current line, and stack info with Python? -


i have logging function follows.

logging.basicconfig(     filename = filename,     format = "%(levelname) -10s %(asctime)s %(message)s",     level = logging.debug )  def printinfo(string):     if debug:         logging.info(string)  def printerror(string):     if debug:         logging.error(string)     print string 

i need login line number, stack information. example:

1: def hello(): 2:    goodbye() 3: 4: def goodbye(): 5:    printinfo()  ---> line 5: goodbye()/hello() 

how can python?

solved

def printinfo(string):     if debug:         frame = inspect.currentframe()         stack_trace = traceback.format_stack(frame)         logging.debug(stack_trace[:-1])     if log:         logging.info(string) 

gives me info need.

debug      2011-02-23 10:09:13,500 [   '  file "/abc.py", line 553, in <module>\n    rununittest(coverage, profile)\n',    '  file "/abc.py", line 411, in rununittest\n    printinfo(string)\n'] 

current function name, module , line number can changing format string include them.

logging.basicconfig(     filename = filename,     format = "%(levelname) -10s %(asctime)s %(module)s:%(lineno)s %(funcname)s %(message)s",     level = logging.debug ) 

most people want stack when logging exception, , logging module automatically if call logging.exception(). if want stack information @ other times need use traceback module extract additional information need.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -