Log to File Example

A snippet showing how to log Django output to a file.


# settings.py

from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "verbose": {"format": "[{asctime}] {levelname} {name} - {message}", "style": "{"},
    },
    "handlers": {
        "file": {
            "class": "logging.FileHandler",
            "filename": BASE_DIR / "django.log",
            "formatter": "verbose",
        },
    },
    "root": {"handlers": ["file"], "level": "INFO"},
}
      
Explanation:
  • Ensure the process user has write permissions to the log path.
  • All logs are written to BASE_DIR/django.log with timestamps and logger names.
  • Category Logging
  • Total Views 323
  • Last Modified 16 June, 2026
  • Tags #logging #file #handlers #debug
Previous snippet
Custom Logger Example
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.