{"id":7168,"library":"django-datadog-logger","title":"Django Datadog Logger","description":"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.","status":"active","version":"0.9.0","language":"en","source_language":"en","source_url":"https://github.com/namespace-ee/django-datadog-logger","tags":["django","datadog","logging","middleware","logs","apm","asgi"],"install":[{"cmd":"pip install django-datadog-logger","lang":"bash","label":"Install stable release"}],"dependencies":[{"reason":"Core framework dependency; officially supports Django 4.2, 5.2, 6.0.","package":"Django","optional":false},{"reason":"Recommended for full APM integration and automatic trace/span ID injection into logs.","package":"ddtrace","optional":true}],"imports":[{"symbol":"RequestIdMiddleware","correct":"from django_datadog_logger.middleware.request_id import RequestIdMiddleware"},{"symbol":"ErrorLoggingMiddleware","correct":"from django_datadog_logger.middleware.error_log import ErrorLoggingMiddleware"},{"symbol":"RequestLoggingMiddleware","correct":"from django_datadog_logger.middleware.request_log import RequestLoggingMiddleware"},{"symbol":"DatadogJSONFormatter","correct":"from django_datadog_logger.formatters.datadog import DatadogJSONFormatter"}],"quickstart":{"code":"import os\n\n# settings.py\n\nMIDDLEWARE = [\n    'django_datadog_logger.middleware.request_id.RequestIdMiddleware',\n    # ... other middlewares ...\n    'django_datadog_logger.middleware.error_log.ErrorLoggingMiddleware',\n    'django_datadog_logger.middleware.request_log.RequestLoggingMiddleware',\n]\n\nLOGGING = {\n    'version': 1,\n    'disable_existing_loggers': False,\n    'formatters': {\n        'datadog_json': {\n            '()': 'django_datadog_logger.formatters.datadog.DatadogJSONFormatter',\n            'format': '%(levelname)s %(asctime)s %(name)s %(message)s',\n        },\n    },\n    'handlers': {\n        'datadog_file': {\n            'class': 'logging.FileHandler',\n            'filename': os.path.join(os.environ.get('LOG_DIR', './'), 'datadog.log'),\n            'formatter': 'datadog_json',\n            'level': 'INFO',\n        },\n        'console': {\n            'class': 'logging.StreamHandler',\n            'formatter': 'datadog_json',\n            'level': 'DEBUG',\n        }\n    },\n    'loggers': {\n        '': {\n            'handlers': ['console', 'datadog_file'],\n            'level': 'INFO',\n            'propagate': True\n        },\n        'django': {\n            'handlers': ['console', 'datadog_file'],\n            'level': 'INFO',\n            'propagate': False,\n        }\n    }\n}","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade to `django-datadog-logger==0.9.0` or later for proper ASGI compatibility. Review your middleware configuration for async contexts.","message":"Version 0.9.0 introduced native async middleware support for ASGI deployments. Prior versions may not fully support asynchronous operations with `RequestIdMiddleware`, `ErrorLoggingMiddleware`, and `RequestLoggingMiddleware`, potentially causing issues or lack of context in async views.","severity":"breaking","affected_versions":"<0.9.0"},{"fix":"Ensure your `LOGGING` configuration specifies `'()': 'django_datadog_logger.formatters.datadog.DatadogJSONFormatter'` for your handlers.","message":"For optimal parsing and full functionality within Datadog Log Management, it is crucial to use a JSON formatter for your logs. `django-datadog-logger` provides `DatadogJSONFormatter` which should be configured in your `LOGGING` settings. Using plain text formatters will result in unparsed or poorly structured logs in Datadog.","severity":"gotcha","affected_versions":"All"},{"fix":"Place `'django_datadog_logger.middleware.request_id.RequestIdMiddleware'` as one of the first entries in your `MIDDLEWARE` list.","message":"The order of middleware in Django's `MIDDLEWARE` setting is important. `RequestIdMiddleware` should typically be placed early in the list (e.g., near the top) to ensure that a request ID is generated and available for all subsequent middleware and application logic that might generate logs.","severity":"gotcha","affected_versions":"All"},{"fix":"Install `ddtrace` (`pip install ddtrace`) and follow its documentation to integrate with your Django application for comprehensive tracing.","message":"While `django-datadog-logger` enhances logging, for full Application Performance Monitoring (APM) integration and automatic injection of `dd.trace_id` and `dd.span_id` into your logs, you should also install and configure the `ddtrace` library from Datadog.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Run `pip install django-datadog-logger` to install the package, and double-check the middleware paths in your `settings.py` against the official documentation.","cause":"The `django-datadog-logger` package is either not installed in your environment or the import path specified in your `settings.py` is incorrect.","error":"ModuleNotFoundError: No module named 'django_datadog_logger.middleware.request_id'"},{"fix":"Ensure the formatter for your Datadog-bound handlers is set to `'()': 'django_datadog_logger.formatters.datadog.DatadogJSONFormatter'`. If using file-based logging, verify your Datadog Agent's `conf.yaml` includes `source: python` and that `log_processing_rules` are correctly configured for JSON.","cause":"Your Django `LOGGING` configuration is not using `DatadogJSONFormatter`, or your Datadog Agent is not configured to process JSON-formatted logs from the specified log file/stream.","error":"Logs in Datadog appear as plain text, or specific log attributes (like `request_id`, `status_code`) are not being parsed correctly into structured fields."},{"fix":"Upgrade to `django-datadog-logger==0.9.0` or a newer version. This version adds `async_capable = True` and `__acall__` implementations to the middleware classes to properly handle ASGI requests.","cause":"You are likely running in an ASGI environment with an older version of `django-datadog-logger` that lacks native async middleware support. Prior to `v0.9.0`, middleware might not correctly interact with async request/response cycles.","error":"Asynchronous views or ASGI applications are not producing logs with request context (e.g., missing `request_id`) or are encountering unexpected errors related to logging middleware."}]}