Skip to content

Instantly share code, notes, and snippets.

Last active June 14, 2023 22:42
  • Star 82 You must be signed in to star a gist
  • Fork 24 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Django logging example
import logging.config
import os
from django.utils.log import DEFAULT_LOGGING
# Disable Django's logging setup
LOGLEVEL = os.environ.get('LOGLEVEL', 'info').upper()
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'default': {
# exact format is not important, this is the minimum information
'format': '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
'django.server': DEFAULT_LOGGING['formatters']['django.server'],
'handlers': {
# console logs to stderr
'console': {
'class': 'logging.StreamHandler',
'formatter': 'default',
# Add Handler for Sentry for `warning` and above
'sentry': {
'level': 'WARNING',
'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
'django.server': DEFAULT_LOGGING['handlers']['django.server'],
'loggers': {
# default for all undefined Python modules
'': {
'level': 'WARNING',
'handlers': ['console', 'sentry'],
# Our application code
'app': {
'level': LOGLEVEL,
'handlers': ['console', 'sentry'],
# Avoid double logging because of root logger
'propagate': False,
# Prevent noisy modules from logging to Sentry
'noisy_module': {
'level': 'ERROR',
'handlers': ['console'],
'propagate': False,
# Default runserver request logging
'django.server': DEFAULT_LOGGING['loggers']['django.server'],
Copy link

kylepw commented Aug 11, 2019

Great article that referenced this. Thanks!

Copy link

this is very good example. How would I create a file based logging configuration in my multi-app django project

Copy link

adi- commented Dec 11, 2019

That is strange, but when I use django.server logger all other loggers stops working. Even django.server logger is not working... any clues?

Copy link

Thank you!

Copy link

dmsergio commented Dec 6, 2022

Thanks for the great content!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment