문제

그래서 개발을 할 때 방금 설정할 수 있습니다. settings.DEBUG 에게 True 오류가 발생하면 스택 추적이 우수하고 정보를 요청하여 잘 형식화 된 것을 볼 수 있습니다.

그러나 일종의 생산 사이트에서는 오히려 사용합니다 DEBUG=False 그리고이 순간 에이 버그를 수정하려는 정보를 가지고 방문자에게 표준 오류 500 페이지를 보여줍니다.)
동시에 서버의 파일에 해당 정보 (추적 및 요청 정보)를 로그인하는 방법이 있습니다. 따라서 콘솔에 출력하고 오류 스크롤을보고 로그를 이메일로 보낼 수 있습니다. 매 시간 또는 이와 같은 것.

이러한 간단한 요구 사항을 충족하는 장고 사이트에 대해 어떤 로깅 솔루션을 추천 하시겠습니까? 응용 프로그램이 실행됩니다 fcgi 서버와 저는 Apache Web Server를 프론트 엔드로 사용하고 있습니다 (LightTPD로 갈 생각이지만).

도움이 되었습니까?

해결책

글쎄, 언제 DEBUG = False, Django는 ADMINS 설정, 알림을 거의 무료로 제공합니다. 보다 세밀한 제어를 원한다면 이름이 지정된 메소드를 정의하는 미들웨어 클래스를 작성하고 추가 할 수 있습니다. process_exception(), 이는 제기 된 예외에 액세스 할 수 있습니다.

http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-exception

당신의 process_exception() 그런 다음 메소드는 원하는 유형의 로깅을 수행 할 수 있습니다 : 콘솔에 쓰기, 파일 쓰기 등 등.

편집 : 조금 덜 유용하지만 got_request_exception 요청 처리 중에 예외가 발생할 때마다 전송되는 신호 :

http://docs.djangoproject.com/en/dev/ref/signals/#got-request-exception

이것은 그렇습니다 ~ 아니다 그러나 예외 객체에 액세스 할 수 있으므로 미들웨어 방법이 훨씬 쉽게 작업 할 수 있습니다.

다른 팁

Django Sentry는 이미 언급했듯이 좋은 방법이지만, 별도의 웹 사이트로 제대로 설정하는 데 약간의 작업이 있습니다. 모든 것을 간단한 텍스트 파일에 로그인하려면 여기에 로깅 구성이 있습니다. settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        # Include the default Django email handler for errors
        # This is what you'd get without configuring logging at all.
        'mail_admins': {
            'class': 'django.utils.log.AdminEmailHandler',
            'level': 'ERROR',
             # But the emails are plain text by default - HTML is nicer
            'include_html': True,
        },
        # Log to a text file that can be rotated by logrotate
        'logfile': {
            'class': 'logging.handlers.WatchedFileHandler',
            'filename': '/var/log/django/myapp.log'
        },
    },
    'loggers': {
        # Again, default Django configuration to email unhandled exceptions
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        # Might as well log any errors anywhere else in Django
        'django': {
            'handlers': ['logfile'],
            'level': 'ERROR',
            'propagate': False,
        },
        # Your own app - this assumes all your logger names start with "myapp."
        'myapp': {
            'handlers': ['logfile'],
            'level': 'WARNING', # Or maybe INFO or DEBUG
            'propagate': False
        },
    },
}

다른 답변에 언급 된 Django-DB-Log는 다음과 같이 대체되었습니다.

https://github.com/dcramer/django-entry

분명히 제임스는 정확하지만 데이터 스토어에 예외를 기록하고 싶다면 이미 사용할 수있는 몇 가지 오픈 소스 솔루션이 있습니다.

1) CrashLog는 좋은 선택입니다. http://code.google.com/p/django-crashlog/

2) DB-Log도 좋은 선택입니다. http://code.google.com/p/django-db-log/

둘의 차이점은 무엇입니까? 내가 볼 수있는 거의 아무것도 없으므로 어느 쪽이든 충분합니다.

나는 둘 다 사용했고 그들은 잘 작동합니다.

EMP의 가장 유용한 코드 제출 이후로 일정 시간이 지났습니다. 방금 구현했고, 약간의 관리 옵션으로 스래싱을하면서 버그를 쫓아 내려고 노력하면서 현재 버전의 django (1.5.?)가 필요로하는 효과에 대한 감가 상각 경고를 받았습니다. Mail_admins 핸들러에 필요합니다.

수정 된 코드는 다음과 같습니다.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
         'require_debug_false': {
             '()': 'django.utils.log.RequireDebugFalse'
         }
     },
    'handlers': {
        # Include the default Django email handler for errors
        # This is what you'd get without configuring logging at all.
        'mail_admins': {
            'class': 'django.utils.log.AdminEmailHandler',
            'level': 'ERROR',
            'filters': ['require_debug_false'],
             # But the emails are plain text by default - HTML is nicer
            'include_html': True,
        },
        # Log to a text file that can be rotated by logrotate
        'logfile': {
            'class': 'logging.handlers.WatchedFileHandler',
            'filename': '/home/username/public_html/djangoprojectname/logfilename.log'
        },
    },
    'loggers': {
        # Again, default Django configuration to email unhandled exceptions
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        # Might as well log any errors anywhere else in Django
        'django': {
            'handlers': ['logfile'],
            'level': 'ERROR',
            'propagate': False,
        },
        # Your own app - this assumes all your logger names start with "myapp."
        'myapp': {
            'handlers': ['logfile'],
            'level': 'DEBUG', # Or maybe INFO or WARNING
            'propagate': False
        },
    },
}

방금 내 성가신 문제가있었습니다 fcgi 스크립트. Django가 시작되기 전에 발생했습니다. 벌목의 부족은 너무 고통 스럽습니다. 어쨌든, Stderr를 파일로 리디렉션하여 가장 먼저 도움이되었습니다.

#!/home/user/env/bin/python
sys.stderr = open('/home/user/fcgi_errors', 'a')
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top