I use this code to write into a .log file when there is an uncaught exception :
import sys
import traceback
def uncaught_exc_handler(ex_cls, ex, tb):
with open('mylog.log', 'w') as f:
traceback.print_last(file=f)
sys.excepthook = uncaught_exc_handler
1/0
Exemple of output :
Traceback (most recent call last):
File "C:\Users\Abc\Desktop\test.py", line 11, in <module>
1/0
ZeroDivisionError: integer division or modulo by zero
How to customize the logging and have this instead :
ERROR 11 (test): ZeroDivisionError: integer division or modulo by zero
?
(note : 11 is the number of the line where the error occured, test is the current file)
PS : I thought about parsing these 4 lines, search for "line" in the second line, extract the int nearby, etc. but that's a rather dirty method, and I imagine this won't work robustly