DataDog Logger
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
- gotcha This library sends log records as DataDog *Events*, not DataDog *Logs*. If you intend to use DataDog's Log Management, you will need a different integration (e.g., DataDog Agent or `datadog-api-client` for Logs API).
- gotcha DataDog API Key and APP Key are mandatory for authentication. Without valid keys, the handler will fail to send events to DataDog.
- gotcha By default, `DataDogHandler` sends events synchronously (buffer_size=1). For high-volume logging, this can impact application performance. Events are flushed upon each log call.
- gotcha Omitting `hostname` or relevant `tags` can make it difficult to filter and analyze events effectively within DataDog.
- breaking Version 1.0.0 introduced a significant rewrite, including the removal of the `python-logstash-logger` dependency and improvements to error handling and tag support. Users upgrading from pre-1.0.0 versions will likely encounter breaking changes.
Install
-
pip install datadog-logger
Imports
- DataDogHandler
from datadog_logger import DataDogHandler
Quickstart
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!')