API Reference
DatadogHTTPHandler
datadog_http_handler.handler.DatadogHTTPHandler
Bases: Handler
High-performance logging handler that sends logs to Datadog via HTTP API.
This handler batches logs and sends them asynchronously to avoid blocking the main application thread. It includes retry logic with exponential backoff, comprehensive error handling, and support for all Datadog sites.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
Optional[str]
|
Datadog API key (or set DD_API_KEY env var) |
None
|
site
|
str
|
Datadog site (default: datadoghq.com) |
'datadoghq.com'
|
service
|
Optional[str]
|
Service name for logs |
None
|
source
|
str
|
Log source (default: python) |
'python'
|
hostname
|
Optional[str]
|
Hostname for logs |
None
|
tags
|
Optional[str]
|
Tags to add to logs (comma-separated string) |
None
|
batch_size
|
int
|
Number of logs to batch before sending |
10
|
flush_interval_seconds
|
float
|
How often to flush logs (seconds) |
5.0
|
timeout_seconds
|
float
|
Request timeout |
10.0
|
max_retries
|
int
|
Maximum retry attempts |
3
|
level
|
int
|
Logging level |
NOTSET
|
Example
Basic usage:
import logging handler = DatadogHTTPHandler( ... api_key="your-api-key", ... service="my-app", ... tags="env:production,team:backend" ... ) logger = logging.getLogger(name) logger.addHandler(handler) logger.info("Application started")
With environment variables:
Set DD_API_KEY, DD_SERVICE, DD_ENV, etc.
handler = DatadogHTTPHandler() # Uses env vars logger = logging.getLogger(name) logger.addHandler(handler)
__init__(api_key=None, site='datadoghq.com', service=None, source='python', hostname=None, tags=None, batch_size=10, flush_interval_seconds=5.0, timeout_seconds=10.0, max_retries=3, level=logging.NOTSET)
Initialize the Datadog HTTP handler.
__repr__()
Return a string representation of the handler.
close()
Close the handler and cleanup resources.
emit(record)
Emit a log record.
flush()
Flush any pending logs.
get_queue_size()
Get the current size of the log queue.
health_check()
Perform a health check to verify the handler is working.
Returns:
| Type | Description |
|---|---|
bool
|
True if the handler is healthy, False otherwise. |