Django Logging

When you encounter a problem, then you should ask log for help

Set up

Set up loggers in your Django project files using the getLogger method of the Python logging package. Next, define logging messages with one of several logger methods: <logger_name>.critical(), <logger_name>.error(), <logger_name>.warning(), <logger_name>.info() or <logger_name>.debug().

Configure the LOGGING variable in settings.py to process logging messages from your logger. Set up different handlers in the LOGGING variable to send logging messages to different destinations. Set up a logging level in either the loggers or handler section of the LOGGING variable to establish a logging threshold.

Concepts and Terms

  1. Loggers: Provide the initial entry point to group logging messages. (Possible for using the same logger across multiple Python modules or .py files)
  2. Handlers: Are used to redirect messages(created by logger) to a destination. Destinations can include flat files, a servers's console, and email or SMS messages.(possible to use more than on handler for the same logger)
  3. Filters: Offer a way to apply fine grained rules on certain logging messages.
  4. Formatter: Are used to specify the final format for log messages.

Django logging

The logging configuration for Django projects is defined in the LOGGING variable in settings.py.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse',
        },
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': {
        'console': {
            'level': 'INFO',  # CRITICAL, ERROR, WARNING, INFO, DEBUG
            'filters': ['require_debug_true'], # Call when debug = True
            'class': 'logging.StreamHandler',  # Logging output to streams such as standard input and standard error  
        },
        'null': {
            'class': 'logging.NullHandler',
        },
        'mail_admins': {
            'level': 'ERROR',   
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
        },
        'django.request': {  
        # indicates that all logging messages associated with it and its children be processed by the main_admins 
        # handler. 
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': False,
        },
        'django.security': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': False,
        },
        'py.warnings': {
            'handlers': ['console'],
        },
    }
}

Create logging messages in Django project

At the top of any Python module or .py file, you can create loggers by using the getLogger method of the logging package. The getLogger method receives the name of the logger as its input parameter.

import logging

# Standard instance if a logger with __name__
stdlogger = logging.getLogger(__name__)  # loggers are automatically created based on the origin of the logging message

# Custom instance logging with explicit name
dbalogger = logging.getLogger('dba')

Don't worry about potentially having dozens of hundreds of loggers in a Django project for each module or .py file. Python logging works with inheritance, so you can define a single handler for a parent logger that handles all children loggers.

  • <Logger_name>.critical() Most severe logging level. Use it to report potentially catastrophic application events (e.g. something that can halt or crash an application)
  • <logger_name>.error() Second most severe logging level. Use it to report important events (e.g. unexpected behaviors or conditions that cause end users to see an error.
  • <logger_name>.warning() Mid-level logging level. Use it to report relatively important events (e.g. unexpected behaviors or conditions that shouldn't happen, yet don't cause end users to notice the issue.
  • <logger_name>.info() Informative logging level. Use it to report informative events in an application (e.g. application milestones or user activity)
  • <logger_name>.debug() Debug logging level. Use it to report step by step logic that can be difficult to write (e.g. complex business logic or database queries)
  • <logger_name>.log() Use it to manually emit logging messages with a specific log level.
  • <logger_name>.exception() Use it to create an error level logging message, wrapped with the current exception stack

'disable_exisiting_loggers': True which disables Django's default loggers from listing. By default, all Python messages follow the '%(levelname)s: %(name)s:%(message)s', which means 'Output the log message level, followed by the name of the logger and the log message itself.'

Log format

Field Syntax Description

%(name)s Name of the logger(logging channel)

%(levelno)s Numeric logging level for the message(DEBUG, INFO, WARNING, ERROR, CRITICAL)

%(levelname)s Text logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL)

%(pathname)s Full pathname of the source file where the logging call was issued.

%(filename)s

results matching ""

    No results matching ""