← Back to Django Course | Chapter 15: Security, Caching & Deployment | Lesson 9 of 9

Logging Basics in Django

Logging is like a ship's logbook -- it quietly writes down what happened so that if something goes wrong later, you can look back and see exactly what led up to it.

Getting a Logger in a View

Each module gets its own logger by name, usually set to __name__. Calling methods like .info() or .error() on it records a message at that severity level.

Example: Getting a Logger in a View

Each module gets its own logger by name, usually set to __name__. Calling methods like .info() or .error() on it records a message at that severity level.

markup
import logging

logger = logging.getLogger(__name__)

def checkout_view(request):
    logger.info('Checkout started for user %s', request.user.id)
    return render(request, 'checkout.html')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Configuring LOGGING in settings.py

The LOGGING setting defines handlers (where messages go) and loggers (which messages get sent where, at what minimum level). A basic console handler is enough to get useful output during development.

Example: Configuring LOGGING in settings.py

The LOGGING setting defines handlers (where messages go) and loggers (which messages get sent where, at what minimum level). A basic console handler is enough to get useful output during development.

markup
# settings.py
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {'console': {'class': 'logging.StreamHandler'}},
    'root': {'handlers': ['console'], 'level': 'INFO'},
}
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Choosing the Right Log Level

DEBUG is for detailed diagnostic info during development, INFO for routine events, WARNING for something unexpected but non-fatal, and ERROR for a failure that needs attention. Picking the right level keeps logs useful instead of noisy.

Note: Reserve ERROR and above for things that actually need someone's attention.

Example: Choosing the Right Log Level

DEBUG is for detailed diagnostic info during development, INFO for routine events, WARNING for something unexpected but non-fatal, and ERROR for a failure that needs attention. Picking the right level keeps logs useful instead of noisy.

markup
import logging

logger = logging.getLogger(__name__)

def process_payment(order):
    logger.debug('Processing payment for order %s', order.id)
    if order.total <= 0:
        logger.warning('Order %s has a non-positive total', order.id)
        return False
    logger.info('Payment succeeded for order %s', order.id)
    return True
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Using print() statements for debugging instead of the logging module, which disappears once the server isn't run from a visible terminal.
  2. Logging everything at the same level, making it impossible to tell routine info from real errors.
  3. Never configuring a logging handler, so log messages silently go nowhere in production.
Chapter Summary
  • Django's logging is configured through the LOGGING dictionary in settings.py, built on Python's standard logging module.
  • Log messages have levels (DEBUG, INFO, WARNING, ERROR, CRITICAL) indicating their severity.
  • Handlers decide where log messages go, such as the console or a file.
  • Using logging instead of print() gives you levels, timestamps, and control over where messages end up.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.