mirror of
https://github.com/MeoProject/lx-music-api-server.git
synced 2025-05-23 19:17:41 +08:00
fix: 修复某些时候因错误无法格式化导致程序崩溃的问题
This commit is contained in:
parent
7ea698e147
commit
fa53fdb4e1
105
common/log.py
105
common/log.py
@ -84,58 +84,65 @@ def stack_info(stack_trace_line):
|
|||||||
sys.stderr.write(f"日志模块出错,本次日志可能无法记录,请报告给开发者: \n" + traceback.format_exc())
|
sys.stderr.write(f"日志模块出错,本次日志可能无法记录,请报告给开发者: \n" + traceback.format_exc())
|
||||||
|
|
||||||
def highlight_error(e):
|
def highlight_error(e):
|
||||||
if (isinstance(e, Exception)):
|
try:
|
||||||
error = stack_error(e)
|
if (isinstance(e, Exception)):
|
||||||
else:
|
error = stack_error(e)
|
||||||
error = e
|
else:
|
||||||
lines = [i.strip() for i in error.split("\n") if i.strip()]
|
error = e
|
||||||
final = []
|
lines = [i.strip() for i in error.split("\n") if i.strip()]
|
||||||
ign = False
|
final = []
|
||||||
for i in lines:
|
ign = False
|
||||||
if (ign):
|
for i in lines:
|
||||||
ign = False
|
if (ign):
|
||||||
continue
|
ign = False
|
||||||
|
|
||||||
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
|
continue
|
||||||
|
|
||||||
code = read_code(p, l)
|
if (i.startswith("Traceback (most recent call last):")):
|
||||||
cc = []
|
final.append(color.cyan(i))
|
||||||
for c in code['result']:
|
elif (i.startswith("During handling of the above exception, another exception occurred:")):
|
||||||
if (c.startswith(code['current'])):
|
final.append(color.cyan(i))
|
||||||
cc.append((' ' * (10 - len(str(l))) + f'{l} >|' + c))
|
elif (i.startswith("The above exception was the direct cause of the following exception:")):
|
||||||
else:
|
final.append(color.cyan(i))
|
||||||
line_number = l + (code["result"].index(c) - 3)
|
elif (i.startswith("File")):
|
||||||
cc.append((' ' * (10 - len(str(line_number))) + f'{line_number} |' + c))
|
ign = True
|
||||||
code = python_highlight("\n".join(cc))
|
p, l, f = stack_info(i)
|
||||||
p = '"' + p + '"'
|
p = p[1:-1]
|
||||||
final.append(f" File {color.yellow(f'{p}')} in {color.cyan(f)}()\n\n\n{code}\n")
|
|
||||||
else:
|
if (p.startswith('<')):
|
||||||
try:
|
final.append(" " + i + '' if (not (lines[lines.index(i) + 1]).startswith("File")) else f"\n{python_highlight(lines[lines.index(i) + 1])}")
|
||||||
if (is_rubbish(i)):
|
|
||||||
continue
|
continue
|
||||||
if (issubclass(require(("builtins." if ("." not in i.split(":")[0]) else "") + i.split(":")[0]), Exception)):
|
|
||||||
exc = i.split(":")[0]
|
code = read_code(p, l)
|
||||||
desc = "" if (len(i.split(":")) == 1) else ':'.join(i.split(":")[1:]).strip()
|
cc = []
|
||||||
final.append(color.red(exc) + (": " + color.yellow(desc)) if (desc) else "")
|
for c in code['result']:
|
||||||
else:
|
if (c.startswith(code['current'])):
|
||||||
final.append(color.cyan(i))
|
cc.append((' ' * (10 - len(str(l))) + f'{l} >|' + c))
|
||||||
except:
|
else:
|
||||||
# traceback.print_exc()
|
line_number = l + (code["result"].index(c) - 3)
|
||||||
final.append(i)
|
cc.append((' ' * (10 - len(str(line_number))) + f'{line_number} |' + c))
|
||||||
return "\n".join(final).replace('\n\n', '\n')
|
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):
|
class LogHelper(logging.Handler):
|
||||||
# 日志转接器
|
# 日志转接器
|
||||||
|
Loading…
x
Reference in New Issue
Block a user