LogDNA Python Logger

raw JSON →
1.18.12 verified Sat May 09 auth: no python

A Python package for sending logs to LogDNA (now Mezmo). This library provides both a standard logging handler and a custom LogDNA logger class. Current version: 1.18.12. Release cadence is irregular, primarily bugfixes.

pip install logdna
error AttributeError: module 'logging' has no attribute 'LogDNAHandler'
cause Trying to import LogDNAHandler as a standard library class.
fix
Use from logdna import LogDNAHandler instead of from logging import LogDNAHandler.
error requests.exceptions.ConnectionError: HTTPSConnectionPool(host='logs.logdna.com', port=443): Max retries exceeded with url: /logs/ingest
cause Network/firewall blocking outbound HTTPS to LogDNA ingestion endpoint, or invalid URL.
fix
Verify your LOGDNA_URL is correct and network allows outbound traffic to logs.logdna.com:443. If using a proxy, configure it via environment variables (e.g., HTTP_PROXY, HTTPS_PROXY).
error logdna.exceptions.LogDNAError: 401 Unauthorized
cause Invalid or missing ingestion key.
fix
Set LOGDNA_INGESTION_KEY environment variable to a valid key. Check that the key is not expired.
breaking In version 1.18.10, authentication was changed to use the 'apikey' HTTP header instead of the 'Authorization' header. Older versions used 'Authorization: Bearer <key>'. This breaks compatibility with setups that relied on the old header format.
fix Upgrade to >= 1.18.10 or ensure your network config allows the 'apikey' header. The library automatically uses the new header; no code change required if using the same key parameter.
gotcha LogDNAHandler.flush() must be called explicitly on program exit to avoid losing buffered logs. The library does not auto-flush on shutdown.
fix Register an atexit handler: `atexit.register(handler.flush)` or flush manually before exit.
gotcha The LogDNAHandler's 'index_meta' parameter defaults to False. If you pass metadata as a dictionary and expect it to be searchable, set index_meta=True explicitly.
fix When creating the handler, include `index_meta=True` to index metadata fields.
deprecated The 'log_error_response' option was introduced in v1.18.3 for debugging but is not officially documented. It silences errors when set to False, which may hide connectivity issues.
fix Avoid using this option in production. If you must, set it to True (default) to see errors.

Minimal setup for sending logs to LogDNA using LogDNAHandler. Replace LOGDNA_INGESTION_KEY with your actual key.

import os
import logging
from logdna import LogDNAHandler

# Use environment variables for credentials
key = os.environ.get('LOGDNA_INGESTION_KEY', '')
# If you have a custom ingestion host, set LOGDNA_URL
url = os.environ.get('LOGDNA_URL', 'https://logs.logdna.com/logs/ingest')

logger = logging.getLogger('logdna')
logger.setLevel(logging.INFO)

handler = LogDNAHandler(
    key=key,
    url=url,
    index_meta=True,  # Optional: index metadata as searchable fields
    tags=['python', 'test'],
    app='myapp',
    env='production',
    level=logging.INFO,
)
logger.addHandler(handler)

logger.info('Hello from LogDNA Python logger!', {"custom": "metadata"})

# Ensure logs are flushed before exit
import atexit
atexit.register(handler.flush)