Django 03편 - Logging
포스트
취소

Django 03편 - Logging

로깅 설정

  • dev_settings.py에 로깅 설정 추가
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'debug_formatter': {
                'format': '[%(levelname)s][%(pathname)s:%(funcName)s():%(lineno)d][%(asctime)s] "%(message)s"',
                'datefmt': '%d/%b/%Y %H:%M:%S'
            },
        },
        'handlers': {
            'debug_console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'debug_formatter',
            }
        },
        'loggers': {
            'debug': {
                'level': 'DEBUG',
                'handlers': ['debug_console']
            },
        },
    }
    
  • 테스트 해보기

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    import logging
    from django.http import HttpResponse
    
    logger = logging.getLogger('debug')
    
    
    # Create your views here.
    def hello_world_view(request):
        logger.debug('This is hello world log.')
        return HttpResponse("Hello world")
    

    dev_settings에서 추가한 debug logger를 이용해 hello world view에 logging을 해보자.

    사진

    굳! hello world 페이지에 접속할 때 console 창에 로그가 잘 찍힌다.

Formatter 키워드

Python logging 글에서 정리한 Formatter Keyword 참고 ;)

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.