fix:修复职业报错的情况
This commit is contained in:
@@ -7,7 +7,7 @@ from fastapi.responses import JSONResponse
|
||||
|
||||
from api.core.exception import ResponseException
|
||||
|
||||
logFormatter = logging.Formatter(
|
||||
logFormatter = logging.Formatter(
|
||||
"""
|
||||
[start]
|
||||
%(asctime)s
|
||||
@@ -15,19 +15,24 @@ logFormatter = logging.Formatter(
|
||||
%(message)s
|
||||
[end]
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
async def all_exception_handler(request: Request, ex: Exception):
|
||||
# api = str(request.url).split("api/")[1]
|
||||
info = ''
|
||||
if not isinstance(ex, ResponseException):
|
||||
info = traceback.format_exc(limit=-1).strip().split('\n')[1:]
|
||||
info[0] = info[0].split('\\')[-1].replace('"', ':').replace(',', '')
|
||||
info = '\r'.join(info)
|
||||
if request.client.host != '127.0.0.1':
|
||||
from fastapi.logger import logger
|
||||
logger.setLevel(logging.ERROR)
|
||||
)
|
||||
|
||||
|
||||
def format_exception() -> str:
|
||||
lines = traceback.format_exc().strip().splitlines()
|
||||
if lines and lines[0].startswith('Traceback '):
|
||||
lines = lines[1:]
|
||||
return '\r'.join(lines)
|
||||
|
||||
|
||||
async def all_exception_handler(request: Request, ex: Exception):
|
||||
# api = str(request.url).split("api/")[1]
|
||||
info = ''
|
||||
if not isinstance(ex, ResponseException):
|
||||
info = format_exception()
|
||||
if request.client.host != '127.0.0.1':
|
||||
from fastapi.logger import logger
|
||||
logger.setLevel(logging.ERROR)
|
||||
logger.name = 'dcalc'
|
||||
log_directory = 'logs'
|
||||
import os
|
||||
@@ -42,12 +47,15 @@ async def all_exception_handler(request: Request, ex: Exception):
|
||||
try:
|
||||
import pathlib
|
||||
pathlib.Path(log_directory).mkdir(parents=True, exist_ok=True)
|
||||
except PermissionError:
|
||||
print('创建日志目录logs失败,请确认是否限制了基础的运行权限')
|
||||
logFunc = logger.error
|
||||
logFunc(info)
|
||||
info = '服务器内部错误'
|
||||
fileHandler.close()
|
||||
except PermissionError:
|
||||
print('创建日志目录logs失败,请确认是否限制了基础的运行权限')
|
||||
logFunc = logger.error
|
||||
try:
|
||||
logFunc(info)
|
||||
info = '服务器内部错误'
|
||||
finally:
|
||||
logger.removeHandler(fileHandler)
|
||||
fileHandler.close()
|
||||
else:
|
||||
info = ex.args[0]
|
||||
# traceback.print_last(sys.exc_info())
|
||||
|
||||
Reference in New Issue
Block a user