Django Datadog Logger

0.9.0 · active · verified Thu Apr 16

Django Datadog Logger is an integration package that provides Django middleware and formatters to send application logs to Datadog. It leverages Python's standard logging module to provide context-rich JSON-formatted logs, including request IDs and error details. The current version is 0.9.0, with a focus on active development and recent support for asynchronous Django (ASGI) environments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart configures `django-datadog-logger` by adding its middleware components to your Django project's `settings.py` to capture request IDs, errors, and access logs. It then sets up a `LOGGING` dictionary to use the `DatadogJSONFormatter` for structured, JSON-formatted output, directing logs to both console and a file for consumption by the Datadog Agent. Remember to set the `LOG_DIR` environment variable or adjust the filename for log storage.

import os

# settings.py

MIDDLEWARE = [
    'django_datadog_logger.middleware.request_id.RequestIdMiddleware',
    # ... other middlewares ...
    'django_datadog_logger.middleware.error_log.ErrorLoggingMiddleware',
    'django_datadog_logger.middleware.request_log.RequestLoggingMiddleware',
]

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'datadog_json': {
            '()': 'django_datadog_logger.formatters.datadog.DatadogJSONFormatter',
            'format': '%(levelname)s %(asctime)s %(name)s %(message)s',
        },
    },
    'handlers': {
        'datadog_file': {
            'class': 'logging.FileHandler',
            'filename': os.path.join(os.environ.get('LOG_DIR', './'), 'datadog.log'),
            'formatter': 'datadog_json',
            'level': 'INFO',
        },
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'datadog_json',
            'level': 'DEBUG',
        }
    },
    'loggers': {
        '': {
            'handlers': ['console', 'datadog_file'],
            'level': 'INFO',
            'propagate': True
        },
        'django': {
            'handlers': ['console', 'datadog_file'],
            'level': 'INFO',
            'propagate': False,
        }
    }
}

view raw JSON →