Django 로그를 파일에 기록하기

Django를 이용하여 프로젝트를 만들고 애플리케이션을 추가한 경우, 기본 설정에서는 로그가 파일에 기록되지 않는다. 로그를 파일에 기록하기 위해서는 setting.py에 있는 'LOGGING' 영역에 로그 파일 설정을 추가해야 한다. 

 
# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOG_FILE = os.path.join(os.path.dirname(__file__), '..''site.log')
LOGGING = {
    'version'1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()''django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level''ERROR',
            'filters': ['require_debug_false'],
            'class''django.utils.log.AdminEmailHandler'
        },
        'logfile': {
            'class''logging.handlers.WatchedFileHandler',
            'filename': LOG_FILE
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level''ERROR',
            'propagate': True,
        },
        # Might as well log any errors anywhere else in Django
        'django': {
            'handlers': ['logfile'],
            'level''DEBUG',
            'propagate': False,
        },
        # Your own app - this assumes all your logger names start with "myapp."
        'timeline': {
            'handlers': ['logfile'],
            'level''DEBUG'# Or maybe INFO or DEBUG
            'propagate': False
        },
    }
}
 
cs


실행 환경

  - OS : Windows 7 Professional K

  - Python : v 2.7.5

  - Django : v 1.4

  - setuptools : v 1.1

  - MariaDB : v 10.1



댓글 쓰기

0 댓글