!6 修改捕获异常后日志记录信息

Merge pull request !6 from 刘建/N/A
This commit is contained in:
ktianc 2023-10-28 13:41:42 +00:00 committed by Gitee
commit dc34f018b3
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -42,8 +42,10 @@ def register_exception(app: FastAPI):
""" """
print("请求地址", request.url.__str__()) print("请求地址", request.url.__str__())
print("捕捉到重写CustomException异常异常custom_exception_handler") print("捕捉到重写CustomException异常异常custom_exception_handler")
logger.error(exc.desc) # logger.error(exc.desc)
logger.error(exc.msg) # logger.error(exc.msg)
# 打印栈信息,方便追踪排查异常
logger.exception(exc)
print(exc.desc) print(exc.desc)
print(exc.msg) print(exc.msg)
return JSONResponse( return JSONResponse(
@ -58,7 +60,9 @@ def register_exception(app: FastAPI):
""" """
print("请求地址", request.url.__str__()) print("请求地址", request.url.__str__())
print("捕捉到重写HTTPException异常异常unicorn_exception_handler") print("捕捉到重写HTTPException异常异常unicorn_exception_handler")
logger.error(exc.detail) # logger.error(exc.detail)
# 打印栈信息,方便追踪排查异常
logger.exception(exc)
print(exc.detail) print(exc.detail)
return JSONResponse( return JSONResponse(
status_code=200, status_code=200,
@ -75,7 +79,9 @@ def register_exception(app: FastAPI):
""" """
print("请求地址", request.url.__str__()) print("请求地址", request.url.__str__())
print("捕捉到重写请求验证异常异常validation_exception_handler") print("捕捉到重写请求验证异常异常validation_exception_handler")
logger.error(exc.errors()) # logger.error(exc.errors())
# 打印栈信息,方便追踪排查异常
logger.exception(exc)
print(exc.errors()) print(exc.errors())
msg = exc.errors()[0].get("msg") msg = exc.errors()[0].get("msg")
if msg == "field required": if msg == "field required":
@ -107,7 +113,9 @@ def register_exception(app: FastAPI):
""" """
print("请求地址", request.url.__str__()) print("请求地址", request.url.__str__())
print("捕捉到值异常value_exception_handler") print("捕捉到值异常value_exception_handler")
logger.error(exc.__str__()) # logger.error(exc.__str__())
# 打印栈信息,方便追踪排查异常
logger.exception(exc)
print(exc.__str__()) print(exc.__str__())
return JSONResponse( return JSONResponse(
status_code=200, status_code=200,
@ -126,7 +134,9 @@ def register_exception(app: FastAPI):
""" """
print("请求地址", request.url.__str__()) print("请求地址", request.url.__str__())
print("捕捉到全局异常all_exception_handler") print("捕捉到全局异常all_exception_handler")
logger.error(exc.__str__()) # logger.error(exc.__str__())
# 打印栈信息,方便追踪排查异常
logger.exception(exc)
return JSONResponse( return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content=jsonable_encoder( content=jsonable_encoder(