exception&log

master
xfc 2 years ago
parent bcd37fa530
commit 4328999efb

@ -158,6 +158,7 @@ REST_FRAMEWORK = {
'DATETIME_FORMAT': "%Y-%m-%d %H:%M:%S",
'DATE_FORMAT': "%Y-%m-%d",
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend'],
'EXCEPTION_HANDLER': 'app.exception.exception_handler',
}
# 配置日志

@ -0,0 +1,23 @@
import logging
from rest_framework.views import exception_handler as drf_exception_handler # drf原生处理异常函数取别名
from rest_framework.views import Response
from rest_framework import status
logger = logging.getLogger('mylogger')
def exception_handler(exc, context):
# drf的exception_handler做基础处理
response = drf_exception_handler(exc, context)
# 为空,进行自定义二次处理
logger.error(str(exc))
if response is None:
# print(exc) # 错误原因
# print(context) # 错误信息
print('%s - %s - %s' % (context['view'], context['request'].method, exc))
return Response({
'detail': '服务器错误'
}, status=status.HTTP_500_INTERNAL_SERVER_ERROR, exception=True)
return response
Loading…
Cancel
Save