Skip to content

Instantly share code, notes, and snippets.

@juancarlospaco
Last active May 22, 2021 23:53
Show Gist options
  • Save juancarlospaco/b9324c1f499620b7628d to your computer and use it in GitHub Desktop.
Save juancarlospaco/b9324c1f499620b7628d to your computer and use it in GitHub Desktop.
Tracebacks friendly pretty printed with more info, good to copy-paste on bug reports.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys, traceback
import logging as log
def log_exception():
"""Log Exceptions but pretty printing with more info, return string."""
unfriendly_names = {"<module>": "Unnamed Anonymous Module Function",
"<stdin>": "System Standard Input Function"}
line_tpl = " |___ {key} = {val} # Type: {t}, Size: {s}Bytes, ID: {i}\n"
body_tpl = """
################################ D E B U G ###############################
Listing all Local objects by context frame, ordered by innermost last:
{body}
Thats all we know about the error, check the LOG file and StdOut.
############################### D E B U G #############################"""
tb, body_txt, whole_txt = sys.exc_info()[2], "", ""
while 1:
if not tb.tb_next:
break
tb = tb.tb_next
stack = []
f = tb.tb_frame
while f:
stack.append(f)
f = f.f_back
stack.reverse()
traceback.print_exc()
for frame in stack:
if frame.f_code.co_name in unfriendly_names.keys():
fun = unfriendly_names[frame.f_code.co_name]
else:
fun = "Function {0}()".format(frame.f_code.co_name)
body_txt += "\nThe {nm} from file {fl} at line {ln} failed!.".format(
nm=fun, fl=frame.f_code.co_filename, ln=frame.f_lineno)
body_txt += "\n {}\n |\n".format(fun)
for key, value in frame.f_locals.items():
whole_txt += line_tpl.format(key=key, val=repr(value)[:50],
t=str(type(value))[:25],
s=sys.getsizeof(key), i=id(value))
result = body_tpl.format(body=body_txt + whole_txt)
log.debug(result)
return result
if __name__ in '__main__':
log.basicConfig(level=-1)
try:
0 / 0
except:
log_exception()
@juancarlospaco
Copy link
Author

juan@z:~$ python recipe.py

Traceback (most recent call last):
  File "/home/juan/recipe.py", line 48, in <module>
    0 / 0
ZeroDivisionError: integer division or modulo by zero

########################## D E B U G #########################
 Listing all Locals by context frame,ordered by innermost last:

The Unnamed Anonymous Module Function from file /home/juan/recipe.py at line 50 failed!.
    Unnamed Anonymous Module Function
    |
    |___ log_exception = <function log_exception at 0x7f6f45691b70>  # Type: <class 'function'>, Size: 62Bytes, ID: 140115882613616.
    |___ __doc__ = None  # Type: <class 'NoneType'>, Size: 56Bytes, ID: 10350432.
    |___ __package__ = None  # Type: <class 'NoneType'>, Size: 60Bytes, ID: 10350432.
    |___ __spec__ = None  # Type: <class 'NoneType'>, Size: 57Bytes, ID: 10350432.
    |___ log = <module 'logging' from '/usr/lib/python3.4/logging  # Type: <class 'module'>, Size: 52Bytes, ID: 140115881687864.
    |___ __builtins__ = <module 'builtins' (built-in)>  # Type: <class 'module'>, Size: 61Bytes, ID: 140115883269160.
    |___ __cached__ = None  # Type: <class 'NoneType'>, Size: 59Bytes, ID: 10350432.
    |___ __loader__ = <_frozen_importlib.SourceFileLoader object at 0x7f  # Type: <class '_frozen_importlib, Size: 59Bytes, ID: 140115882412016.
    |___ __name__ = '__main__'  # Type: <class 'str'>, Size: 57Bytes, ID: 140115882353776.
    |___ sys = <module 'sys' (built-in)>  # Type: <class 'module'>, Size: 52Bytes, ID: 140115882828008.
    |___ __file__ = '/home/juan/code/temp.py'  # Type: <class 'str'>, Size: 57Bytes, ID: 140115881679728.
    |___ traceback = <module 'traceback' from '/usr/lib/python3.4/trace  # Type: <class 'module'>, Size: 58Bytes, ID: 140115881686344.


 Thats all we know about the error, check the LOG file.
########################## D E B U G #########################
juan@z:~$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment