Browse Source

added the decortaor to record logs

dev
Jingxun 8 months ago
parent
commit
07ad66ff26
  1. 51
      backend/decorators/exception_log.py

51
backend/decorators/exception_log.py

@ -0,0 +1,51 @@
# encoding: utf-8
"""
@author: Qiancj
@contact: qiancj@risenenergy.com
@file: exception_log
@create-time: 2023-09-26 09:55
@description: The new python script
"""
import traceback
from functools import wraps
class DecratorSet:
@staticmethod
def log_dec(func):
@wraps(func)
def inner(*args, **kwargs):
status = False
out = {"code": 400500, "msg": "程序内部未知错误"}
try:
out = func(*args, **kwargs)
status = True
except Exception as e:
status = False
err = repr(e)
msg_content = traceback.format_exc()
# msg_content_list = msg_content.split("\n")
# pattern = re.compile(r'^\s*File\s*\S+[\\/]models\.py(\S*\s*)')
# msg_list = [i for i in msg_content_list if pattern.match(i)]
# location = msg_list[-1].strip() if msg_list else "未知错误"
out = {"status": 500500, "data": None, "msg": msg_content}
finally:
if not status:
func_name = func.__name__
# redis.init_conn()
# redis.pub(
# "real_pub",
# str({
# "func": func_name,
# "err": err,
# "params": msg_param,
# "location": location,
# "details": msg_content
# })
# )
# redis.close()
return out
return inner
Loading…
Cancel
Save