import json import traceback import os import logging FILENAME = "./traceback.json" def prechecker(): if not os.path.exists(FILENAME): with open(FILENAME, "w", encoding = "utf-8") as f: json.dump({"err":{}, "id":0}, f, ensure_ascii=False) def load(): prechecker() with open(FILENAME, "r", encoding = "utf-8") as f: d = json.load(f) return d def debug_info_from_exception(exc) -> dict: exc_type = type(exc).__name__ exc_message = str(exc) exc_traceback = traceback.format_exception(type(exc), exc, exc.__traceback__) debug_info = { "Exception_type": str(exc_type), "Exception_message": str(exc_message), "Trackback": str(exc_traceback) } # debug for s in exc_traceback: logging.error(s) # must display return debug_info def write(e:Exception): d:dict = load() eid = d["id"] debug_info = debug_info_from_exception(e) d["err"][str(eid)] = debug_info d["id"] += 1 with open(FILENAME, "w", encoding = "utf-8") as f: json.dump(d, f, ensure_ascii=False) return eid def easyExceptionHandler(e:Exception): exc_type = type(e).__name__ exc_message = str(e) exc_saved_id = write(e) logging.error(f"Exception id {exc_saved_id} : {exc_type} : {exc_message}")