Log HTTP 5xx responses in Django
By default, in a production environment where setting DEBUG is false, Django sends log messages with ERROR or CRITICAL levels to AdminEmailHandler. This includes HTTP 5xx responses, which are handled by the django.request logger.
If you don’t have outbound email configured you lose these errors.
Python’s logging.StreamHandler defaults to sending output to stderr, and most of the time my Django applications are managed by systemd service units which default to passing stdout and stderr to systemd’s journal.
Put all of that knowledge together and here’s the delightfully simple LOGGING configuration I usually add to my production settings:
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"stderr": {
"class": "logging.StreamHandler"
}
},
"loggers": {
"django.request": {
"level": "ERROR",
"handlers": ["stderr"]
}
}
}
I can then fumble about with journald as required.


