Monday, 20 November 2023

django handling 500 error with logs

 https://stackoverflow.com/questions/59332225/hitting-500-error-on-django-with-debug-false-even-with-allowed-hosts


To quickly find out exception trace, you can quickly turn on 

  1. Put DEBUG=True in the project/settings.py


although this should be false in prod, but just for quick show of error


The proper way is to set up logger to send admin email and produce 500 in a log file (TODO)
https://stackoverflow.com/questions/238081/how-do-you-log-server-errors-on-django-sites

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
        },
    },
}


*send email need more research
https://stackoverflow.com/questions/38131255/logging-and-email-not-working-for-django-for-500
Send emails:
from os.path import join

# Email Configuration ==========================================================
ADMINS = [('Me', 'my@email'), ]
MANAGERS = ADMINS
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = 'My Name To Display <my@email>'
SERVER_EMAIL = 'error_from@email'
EMAIL_HOST = 'smtp.server'  # An IP may work better
EMAIL_HOST_USER = 'username_for_login'
EMAIL_HOST_PASSWORD = 'password_for_login'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

# Logging Configuration ========================================================
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'file': {
            'level': 'ERROR',
            'class': 'logging.FileHandler',
            'filename': join(HOME_DIR, 'my_logs', 'debug.log'),
            'formatter': 'verbose'
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'formatter': 'simple'
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'ERROR',
            'propagate': True,
        },
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    },
}

No comments:

Post a Comment