DJango 오류 로그 생성

2736 단어
DJango 오류 로그 생성
setting.py 설정
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(lineno)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(module)s %(lineno)d %(message)s'
        },
    },
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'file': {
            #         WARNING
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            #     ,     ,            , :          BASE_DIR
            'filename': os.path.join(os.path.dirname(BASE_DIR), "logs/luffy.log"),
            #         ,      300M
            'maxBytes': 300 * 1024 * 1024,
            #        ,         10
            'backupCount': 10,
            #     :    
            'formatter': 'verbose',
            #   
            'encoding': 'utf-8'
        },
    },
    #     
    'loggers': {
        'django': {
            'level': 'INFO',
            'handlers': ['console', 'file'],
            'propagate': True,  #                      
        },
    }
}

exception.py(오류 설정)
# rest_framework.views    exception_handler       drf        
from rest_framework.views import exception_handler as drf_exception_handler
# drf        drf_exception_handler,        drf      
from rest_framework.response import Response
from .response import APIResponse
#           :    logging       
from .logging import logger
def exception_handler(exc, context):
    response = drf_exception_handler(exc, context)

    if response is None:
        # drf       
        error_info = '【%s】【%s】' % (context['view'], exc)
        logger.error(error_info)
        # return Response({
        #     'exception': '     ',
        # }, status=500)
        return APIResponse(1, '     ', status=500)

    response.exception = True
    return response

logging.py
import logging
logger = logging.getLogger('django')

좋은 웹페이지 즐겨찾기