From fa53fdb4e1ad937c6d01dd48a5f1642ba5d5d356 Mon Sep 17 00:00:00 2001 From: helloplhm-qwq Date: Sun, 3 Mar 2024 14:03:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=9F=90=E4=BA=9B?= =?UTF-8?q?=E6=97=B6=E5=80=99=E5=9B=A0=E9=94=99=E8=AF=AF=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=AF=BC=E8=87=B4=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E5=B4=A9=E6=BA=83=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/log.py | 105 +++++++++++++++++++++++++++----------------------- 1 file changed, 56 insertions(+), 49 deletions(-) diff --git a/common/log.py b/common/log.py index b436cc3..f68363a 100644 --- a/common/log.py +++ b/common/log.py @@ -84,58 +84,65 @@ def stack_info(stack_trace_line): sys.stderr.write(f"日志模块出错,本次日志可能无法记录,请报告给开发者: \n" + traceback.format_exc()) def highlight_error(e): - if (isinstance(e, Exception)): - error = stack_error(e) - else: - error = e - lines = [i.strip() for i in error.split("\n") if i.strip()] - final = [] - ign = False - for i in lines: - if (ign): - ign = False - continue - - if (i.startswith("Traceback (most recent call last):")): - final.append(color.cyan(i)) - elif (i.startswith("During handling of the above exception, another exception occurred:")): - final.append(color.cyan(i)) - elif (i.startswith("The above exception was the direct cause of the following exception:")): - final.append(color.cyan(i)) - elif (i.startswith("File")): - ign = True - p, l, f = stack_info(i) - p = p[1:-1] - - if (p.startswith('<')): - final.append(" " + i + '' if (not (lines[lines.index(i) + 1]).startswith("File")) else f"\n{python_highlight(lines[lines.index(i) + 1])}") + try: + if (isinstance(e, Exception)): + error = stack_error(e) + else: + error = e + lines = [i.strip() for i in error.split("\n") if i.strip()] + final = [] + ign = False + for i in lines: + if (ign): + ign = False continue - code = read_code(p, l) - cc = [] - for c in code['result']: - if (c.startswith(code['current'])): - cc.append((' ' * (10 - len(str(l))) + f'{l} >|' + c)) - else: - line_number = l + (code["result"].index(c) - 3) - cc.append((' ' * (10 - len(str(line_number))) + f'{line_number} |' + c)) - code = python_highlight("\n".join(cc)) - p = '"' + p + '"' - final.append(f" File {color.yellow(f'{p}')} in {color.cyan(f)}()\n\n\n{code}\n") - else: - try: - if (is_rubbish(i)): + if (i.startswith("Traceback (most recent call last):")): + final.append(color.cyan(i)) + elif (i.startswith("During handling of the above exception, another exception occurred:")): + final.append(color.cyan(i)) + elif (i.startswith("The above exception was the direct cause of the following exception:")): + final.append(color.cyan(i)) + elif (i.startswith("File")): + ign = True + p, l, f = stack_info(i) + p = p[1:-1] + + if (p.startswith('<')): + final.append(" " + i + '' if (not (lines[lines.index(i) + 1]).startswith("File")) else f"\n{python_highlight(lines[lines.index(i) + 1])}") continue - if (issubclass(require(("builtins." if ("." not in i.split(":")[0]) else "") + i.split(":")[0]), Exception)): - exc = i.split(":")[0] - desc = "" if (len(i.split(":")) == 1) else ':'.join(i.split(":")[1:]).strip() - final.append(color.red(exc) + (": " + color.yellow(desc)) if (desc) else "") - else: - final.append(color.cyan(i)) - except: - # traceback.print_exc() - final.append(i) - return "\n".join(final).replace('\n\n', '\n') + + code = read_code(p, l) + cc = [] + for c in code['result']: + if (c.startswith(code['current'])): + cc.append((' ' * (10 - len(str(l))) + f'{l} >|' + c)) + else: + line_number = l + (code["result"].index(c) - 3) + cc.append((' ' * (10 - len(str(line_number))) + f'{line_number} |' + c)) + code = python_highlight("\n".join(cc)) + p = '"' + p + '"' + final.append(f" File {color.yellow(f'{p}')} in {color.cyan(f)}()\n\n\n{code}\n") + else: + try: + if (is_rubbish(i)): + continue + if (issubclass(require(("builtins." if ("." not in i.split(":")[0]) else "") + i.split(":")[0]), Exception)): + exc = i.split(":")[0] + desc = "" if (len(i.split(":")) == 1) else ':'.join(i.split(":")[1:]).strip() + final.append(color.red(exc) + (": " + color.yellow(desc)) if (desc) else "") + else: + final.append(color.cyan(i)) + except: + # traceback.print_exc() + final.append(i) + return "\n".join(final).replace('\n\n', '\n') + except: + logger.error('格式化错误失败,使用默认格式\n' + traceback.format_exc()) + if (isinstance(e, Exception)): + return stack_error(e) + else: + return e class LogHelper(logging.Handler): # 日志转接器