Created
March 11, 2025 19:20
-
-
Save cnmoro/3e704befb470ba0a1769c387b3e7bd22 to your computer and use it in GitHub Desktop.
log_exception
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import traceback, inspect, re, datetime | |
def log_exception(e, custom_msg=None): | |
# Get the frame of the caller (the function where the exception was caught) | |
frame = inspect.currentframe().f_back | |
func_name = frame.f_code.co_name | |
filename = frame.f_code.co_filename | |
line_no = frame.f_lineno | |
# Retrieve the argument names and values from the caller's frame. | |
arg_info = inspect.getargvalues(frame) | |
# This gives you a tuple: (args, varargs, keywords, locals) | |
# We'll only capture the explicit parameters (args). | |
params = {arg: arg_info.locals.get(arg) for arg in arg_info.args} | |
detailed_exp = traceback.format_exc() | |
msg = f"{get_datetime_brasilia()} ~ Erro {custom_msg} (Chamada do log: [{filename}.{func_name}():{line_no}]).\nErro completo: {detailed_exp}" | |
print(msg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment