Skip to main content

Django Datadog Logger integration package.

Project description

Django Datadog Logger

image CI Checks Black Documentation Status

Django Datadog Logger integration package.

Quick start

Set up request id tracking (in front) and logging middlewares (at the end):

MIDDLEWARE = [
    "django_datadog_logger.middleware.request_id.RequestIdMiddleware",
    # ...
    "django_datadog_logger.middleware.error_log.ErrorLoggingMiddleware",
    "django_datadog_logger.middleware.request_log.RequestLoggingMiddleware",
]

Configure LOGGERS in your Django settings file:

API_LOG_ROOT = env.str("API_LOG_ROOT")
LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "console": {"format": "{levelname} {message}", "style": "{"},
        "json": {"()": "django_datadog_logger.formatters.datadog.DatadogJSONFormatter"},
    },
    "handlers": {
        "console": {"level": "INFO", "class": "logging.StreamHandler", "formatter": "console"},
        "application": {
            "level": API_LOG_APPLICATION_LEVEL,
            "class": "logging.FileHandler",
            "filename": os.path.join(API_LOG_ROOT, "api.application.log"),
            "formatter": "json",
        },
        "state": {
            "level": API_LOG_STATE_LEVEL,
            "class": "logging.FileHandler",
            "filename": os.path.join(API_LOG_ROOT, "api.state.log"),
            "formatter": "json",
        },
        "request": {
            "level": API_LOG_REQUEST_LEVEL,
            "class": "logging.FileHandler",
            "filename": os.path.join(API_LOG_ROOT, "api.request.log"),
            "formatter": "json",
        },
        "session": {
            "level": API_LOG_SESSION_LEVEL,
            "class": "logging.FileHandler",
            "filename": os.path.join(API_LOG_ROOT, "api.session.log"),
            "formatter": "json",
        },
        "error": {
            "level": API_LOG_ERROR_LEVEL,
            "class": "logging.FileHandler",
            "filename": os.path.join(API_LOG_ROOT, "api.error.log"),
            "formatter": "json",
        },
    },
    "loggers": {
        "": {"handlers": ["console", "error"], "level": "DEBUG", "propagate": True},
        "ddtrace": {"handlers": ["error"], "level": "ERROR", "propagate": False},
        "django.db.backends": {"handlers": ["error"], "level": "ERROR", "propagate": False},
        "twilio": {"handlers": ["error"], "level": "ERROR", "propagate": False},
        "my_project": {"handlers": ["application"], "level": "INFO", "propagate": False},
        "my_project.throttling": {"handlers": ["application"], "level": "DEBUG", "propagate": False},
        "my_project.vehicles.viewsets.state": {"handlers": ["state"], "level": "INFO", "propagate": False},
        "my_project.accounts.session": {"handlers": ["session"], "level": "DEBUG", "propagate": False},
        "my_project.session": {"handlers": ["session"], "level": "DEBUG", "propagate": False},
        "django_auth_ldap": {"level": "DEBUG", "handlers": ["session"], "propagate": False},
        "django_datadog_logger.middleware.error_log": {"handlers": ["error"], "level": "INFO", "propagate": False},
        "django_datadog_logger.middleware.request_log": {"handlers": ["request"], "level": "INFO", "propagate": False},
        "django_datadog_logger.rest_framework": {"handlers": ["application"], "level": "INFO", "propagate": False},
    },
}

If you would like to whitelist your projects for passing extra arguments to the json log record, please set the following regular expression:

DJANGO_DATADOG_LOGGER_EXTRA_INCLUDE = r"^(django_datadog_logger|my_project)(|\..+)$"

Add Celery logger configuration and request_id tracking decorator to tasks:

import logging

from celery import Celery, shared_task
from celery.result import AsyncResult
from celery.signals import after_setup_logger, after_setup_task_logger
from django.conf import settings
from django_datadog_logger.celery import store_celery_request

logger = logging.getLogger(__name__)


@after_setup_logger.connect
def on_after_setup_logger(logger, *args, **kwargs):
    from django_datadog_logger.formatters.datadog import DatadogJSONFormatter

    if settings.API_LOG_CELERY_JSON:
        formatter = DatadogJSONFormatter()
        for handler in list(logger.handlers):
            handler.setFormatter(formatter)
            handler.setLevel(settings.API_LOG_CELERY_LEVEL)


@after_setup_task_logger.connect
def on_after_setup_task_logger(logger, *args, **kwargs):
    from django_datadog_logger.formatters.datadog import DatadogJSONFormatter

    if settings.API_LOG_CELERY_JSON:
        formatter = DatadogJSONFormatter()
        for handler in list(logger.handlers):
            handler.setFormatter(formatter)
            handler.setLevel(settings.API_LOG_CELERY_LEVEL)


app = Celery("my_project")

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object("django.conf:settings", namespace="CELERY")
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@shared_task(bind=True)
@store_celery_request
def debug_task(self):
    print("Request: {0!r}".format(self.request))
    logger.critical("CRITICAL", extra={"level": "CRITICAL"})
    logger.error("ERROR", extra={"level": "ERROR"})
    logger.warning("WARNING", extra={"level": "WARNING"})
    logger.info("INFO", extra={"level": "INFO"})
    logger.debug("DEBUG", extra={"level": "DEBUG"})
    return 42

ddtrace

The ddtrace library has an option to inject tracing context data into log records: https://ddtrace.readthedocs.io/en/stable/advanced_usage.html#logs-injection

There is a helper to look for those attributes and add them automatically to the log entry created by this library.

# log.py

# Patch logging library to inject dd.* attributes on log records
import ddtrace
ddtrace.patch(logging=True)

# Configure logger with DatadogJSONFormatter
import logging
from django_datadog_logger.formatters.datadog import DatadogJSONFormatter

logger = logging.root

handler = logging.StreamHandler()
handler.formatter = DatadogJSONFormatter()
logger.addHandler(handler)
logger.setLevel(logging.INFO)


# Log a test message
logger.info("test")
$ DD_SERVICE=django DD_ENV=test DD_VERSION=1234 python log.py
{"message": "test", "logger.name": "root", "logger.thread_name": "MainThread", "logger.method_name": "<module>", "syslog.timestamp": "2021-08-23T18:26:10.391099+00:00", "syslog.severity": "INFO", "dd.version": "1234", "dd.env": "test", "dd.service": "django", "dd.trace_id": "0", "dd.span_id": "0"}

If you remove the call to datadog.patch(logging=True) you end up with:

$ python test.py
{"message": "test", "logger.name": "root", "logger.thread_name": "MainThread", "logger.method_name": "<module>", "syslog.timestamp": "2021-08-23T18:27:47.951461+00:00", "syslog.severity": "INFO"}

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_datadog_logger-0.9.0.tar.gz (73.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_datadog_logger-0.9.0-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file django_datadog_logger-0.9.0.tar.gz.

File metadata

  • Download URL: django_datadog_logger-0.9.0.tar.gz
  • Upload date:
  • Size: 73.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_datadog_logger-0.9.0.tar.gz
Algorithm Hash digest
SHA256 502ccdf7684f29f4ca40c52ed1d03484236760abc71568f032d766aee42bc9bb
MD5 1bd77ff13368ade3c1b7a3cd6e1cf0e3
BLAKE2b-256 06b236f165f1bf237ebffd1f14559b45133161e2a0570588adac43ec0db1b5c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_datadog_logger-0.9.0.tar.gz:

Publisher: build.yml on namespace-ee/django-datadog-logger

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_datadog_logger-0.9.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_datadog_logger-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8bdb40f5dc7c7a8095e301bbaf623dbdad4c141dadb4b9cdccfebc68df130f3a
MD5 95215229c074066f16edb1f12b946392
BLAKE2b-256 9cf0061da061fc91add12797cbc8c6a31a10407f631f12475f95b64103af8e6f

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_datadog_logger-0.9.0-py3-none-any.whl:

Publisher: build.yml on namespace-ee/django-datadog-logger

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page