Configure Django Logging

A snippet showing how to configure logging in Django settings.py.


# settings.py

import os

LOG_LEVEL = os.environ.get("DJANGO_LOG_LEVEL", "INFO")

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "verbose": {
            "format": "[{asctime}] {levelname} {name}: {message}",
            "style": "{",
        },
        "simple": {
            "format": "{levelname}: {message}",
            "style": "{",
        },
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        },
        "file": {
            "class": "logging.FileHandler",
            "filename": BASE_DIR / "django.log",
            "formatter": "verbose",
        },
    },
    "root": {
        "handlers": ["console", "file"],
        "level": LOG_LEVEL,
    },
    "loggers": {
        "django": {
            "handlers": ["console"],
            "level": LOG_LEVEL,
            "propagate": False,
        },
        "myapp": {
            "handlers": ["console", "file"],
            "level": "DEBUG",
            "propagate": False,
        },
    },
}
      
Explanation:
  • Configure multiple formatters (verbose, simple) to control log output style.
  • Use both console and file handlers to capture logs in dev and production.
  • Loggers allow per-app granularity (e.g., "myapp" logs DEBUG while root uses INFO).
  • Control global log level with DJANGO_LOG_LEVEL env var.
  • Category Deployment & Settings
  • Total Views 1156
  • Last Modified 12 December, 2025
  • Tags #settings #logging #debug #deployment
Previous snippet
Configure Allowed Hosts
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.