0

I need to know how I can log an exception stack trace along with the method arguments' actual values.

To clarify my requirement, please refer to following example:

Code Sample

import logging
def a(str, str2):
    print str + str2
    raise Exception("Custom err ==> " + str + "----" + str2)
def b(str):
    a(str, "World!")
def c(str):
    b(str)
try:
    val = 'Hello' #Let's say this value is coming from DB
    c(val)
except:
    logging.exception("err", exc_info=True)

Actual Stack trace in Python

HelloWorld!
ERROR:root:err
Traceback (most recent call last):
  File "except.py", line 14, in <module>
    c('Hello')
  File "except.py", line 11, in c
    b(str)
  File "except.py", line 8, in b
    a(str, "World!")
  File "except.py", line 5, in a
    raise Exception("Custom err ==> " + str + "----" + str2)
Exception: Custom err ==> Hello----World!

Required Stack trace in Python

HelloWorld!
ERROR:root:err
Traceback (most recent call last):
  File "except.py", line 14, in <module>
    c('Hello')
  File "except.py", line 11, in c
    b('Hello')
  File "except.py", line 8, in b
    a('Hello', "World!")
  File "except.py", line 5, in a
    raise Exception("Custom err ==> " + str + "----" + str2)
Exception: Custom err ==> Hello----World!

If you looked carefully in the Required Stack trace in Python section, I have replaced the evaluated values of method arguments in stack trace.

I hope this example gives clear perspective of my requirements

1 Answers1

0

You probably should not use str as name but that is beside the point.

You can't have your desire - the logging module does not print values it prints the names used as params. See changed program (most variable names changed):

import logging
def a(var_name_in_a, var_name_in_a_2):
    print var_name_in_a + var_name_in_a_2
    raise Exception("Custom err ==> " + var_name_in_a + "----" + var_name_in_a_2)
def b(var_name_in_b):
    a(var_name_in_b, "World!")
def c(var_name):
    b(var_name)
try:
    val = 'Hello' #Let's say this value is coming from DB
    c(val)
except:
    logging.exception("stdout", exc_info=True)

and its output:

HelloWorld!
ERROR:root:err
Traceback (most recent call last):
  File "test.py", line 11, in <module>
    c(val)
  File "test.py", line 8, in c
    b(var_name)
  File "test.py", line 6, in b
    a(var_name_in_b, "World!")
  File "test.py", line 4, in a
    raise Exception("Custom err ==> " + var_name_in_a + "----" + var_name_in_a_2)
Exception: Custom err ==> Hello----World!

If you want the values you would have to logg them separately from the exception - and before it happens - afterwards this information is lost.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Appologies for late reply. That's sad, I generally use exception stack trace for my first level of debugging. In PHP I could see what values were passed as arguments to a function call. Which gives me a clear picture of what went wrong in the first place. I hoped python also includes similar exception logging – Lankesh Zade Mar 05 '19 at 08:55