0

My Django app is only properly logging and emailing errors when Debug=True.

On production, I'm only getting 'Invalid HTTP_HOST header' errors in both the log file and my inbox. Every other error is not logged in the django log nor is it emailed to me.

I've tried some things with this test request:

def test(request):
    logging.debug('debug test message')  // Always appears in log file
    from django.core.mail import mail_admins
    mail_admins('test error', 'test error via call to mail_admins()') // Always arrives in my inbox just fine
    raise Appointment.DoesNotExist  // When Debug=False (both locally and in production), does not appear in log file or in inbox.

My logging settings for both local and production:

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'
        },
        'csv_formatter': {
            'format': '%(levelname)s;%(asctime)s;%(message)s'
        }
    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'logging.NullHandler',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        },
        'default': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'maxBytes': 1024 * 1024 * 50,
            'backupCount': 10,
            'formatter': 'csv_formatter',
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'formatter': 'verbose',
        },
    },
    'loggers': {
        '': {
            'handlers': ['default'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'asyncio': {
            'level': 'WARNING',
        },
        'django.request': {
           'handlers': ['mail_admins'],
           'level': 'ERROR',
           'propagate': True,
        },
        'django.security': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

How do I make sure all Django errors are logged and emailed in production (when Debug=False)?

Divisible by Zero
  • 2,616
  • 28
  • 31
  • https://stackoverflow.com/questions/25370868/django-error-invalid-http-host-header-u-run-myprojectname-gunicorn-sock – Jisson Jan 10 '22 at 12:52
  • @Jisson, my question is not about getting invalid host header error emails. It's about getting none of the errors that actually matter! :) – Divisible by Zero Jan 10 '22 at 13:00
  • 1
    Have a look [here](https://docs.djangoproject.com/en/dev/ref/logging/#default-logging-definition) for the default logging configuration that django uses. Note that this is merged with your logging config. In this case if you want `raise Appointment.DoesNotExist` to be logged to the console when debug is `False`, you will have to override the console handler's `filters` as it currently requires `debug` to be true. Try to set it to just `[]`. About the admin mailing: In your function, if you remove `mail_admins()` and set debug to `True`, does `raise Appointment.DoesNotExist` send an email? – Brian Destura Jan 16 '22 at 23:28
  • 2
    Have a look at the documentation : https://docs.djangoproject.com/en/3.2/topics/logging/#id3 I presume that with `DEBUG=True`, you fire the empty logger that calls the default handler. With `DEBUG=False`, you only trigger the HTTP (django.request) and Security (django.security) errors. What if you create a `django` logger ? – DKH Jan 17 '22 at 14:26
  • not really a solution here but for monitoring something in production I'd recommend using monitoring tools like Sentry – rabbit.aaron Jan 23 '22 at 06:16
  • I could not reproduce this. Which version of Django are you on? – aaron Jan 23 '22 at 13:09
  • @aaron Django 3.2 – Divisible by Zero Jan 24 '22 at 11:49
  • I'm on Django 3.2.11 and could not reproduce it. Can you reproduce it locally, or only on your production server? – aaron Jan 28 '22 at 06:25
  • @aaron Thanks for trying. It happens both locally and in production. – Divisible by Zero Jan 28 '22 at 11:04
  • Can you reproduce it with a new project and share it on GitHub? – aaron Jan 28 '22 at 11:07

0 Answers0