DataDog Logger

1.0.2 · active · verified Mon Apr 13

The datadog-logger library provides a Python logging handler designed to send log records as events to DataDog. It simplifies integrating Python application logs with DataDog's event stream for monitoring and alerting. The current version is 1.0.2, and releases are infrequent, primarily focused on maintenance and minor improvements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a `DataDogHandler` and integrate it with a standard Python `logging` instance. It configures the handler with API/APP keys, a hostname, and custom tags. Remember to replace placeholder keys with actual DataDog credentials, ideally loaded from environment variables.

import logging
import os
from datadog_logger import DataDogHandler

# Configure your DataDog API and APP keys
DD_API_KEY = os.environ.get('DATADOG_API_KEY', 'YOUR_DATADOG_API_KEY')
DD_APP_KEY = os.environ.get('DATADOG_APP_KEY', 'YOUR_DATADOG_APP_KEY')

# Set up a standard Python logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# Configure the DataDogHandler
# For production, set a meaningful hostname and tags
datadog_handler = DataDogHandler(
    api_key=DD_API_KEY,
    app_key=DD_APP_KEY,
    hostname='my_application_host',
    tags=['env:dev', 'service:example-app']
)

# Add the handler to your logger
logger.addHandler(datadog_handler)

# Log some messages
logger.info('Application started successfully.')
logger.warning('Potential issue detected in module X.',
               extra={'user_id': 456, 'module': 'auth'})
logger.error('Critical error: Database connection failed!')

view raw JSON →