Skip to main content

A structured JSON logging formatter with Datadog integration

Project description

Cobli Logging Formatter

A structured JSON logging formatter with Datadog integration for Python applications.

Features

  • Structured JSON Logging: Outputs logs in a consistent JSON format
  • Datadog Integration: Automatically includes trace and span IDs from Datadog APM
  • Timezone Aware: Timestamps in UTC with ISO 8601 format
  • Custom Fields Support: Automatically captures and includes custom log fields
  • Thread Information: Includes thread names for multi-threaded applications
  • Exception Handling: Captures and formats stack traces
  • Environment Configuration: Uses environment variables for service metadata

Installation

Using uv (recommended)

uv add cobli-logging-formatter

Using pip

pip install cobli-logging-formatter

Quick Start

Option 1: Use the preconfigured logger

from cobli_logging import get_logger

logger = get_logger()
logger.info("Hello, world!")
logger.error("Something went wrong", extra={"user_id": 123, "action": "login"})

Option 2: Use just the formatter

import logging
from cobli_logging import JsonFormatter

logger = logging.getLogger("my-service")
handler = logging.StreamHandler()
handler.setFormatter(JsonFormatter())
logger.addHandler(handler)
logger.setLevel(logging.INFO)

logger.info("Hello, world!")

Option 3: Use the configuration helper

from cobli_logging import configure_logging

# Configure with default settings
configure_logging()

# Or with custom settings
configure_logging(
    service_name="my-custom-service",
    log_level="DEBUG",
    propagate=True
)

import logging
logger = logging.getLogger("my-custom-service")
logger.info("Configured logger ready!")

Configuration

The formatter uses the following environment variables:

  • DD_SERVICE: Service name for Datadog (optional)
  • DD_VERSION: Service version for Datadog (optional)
  • LOG_LEVEL: Logging level (default: "INFO")

Output Format

{
  "timestamp": "2025-06-05T10:30:00Z",
  "level": "INFO",
  "message": "User logged in successfully",
  "thread_name": "WorkerThread-1",
  "dd": {
    "trace_id": "1234567890123456789",
    "span_id": "987654321",
    "service": "user-service",
    "version": "1.2.3"
  },
  "custom": {
    "user_id": 123,
    "action": "login",
    "ip_address": "192.168.1.1"
  },
  "stack_trace": "Traceback (most recent call last):\n..."
}

Development

Quick start with Makefile

This project uses a comprehensive Makefile for all development tasks:

# Complete development setup
make dev-setup

# Run all quality checks (format, lint, test)
make check

# Run tests with coverage
make test-cov

# Build the package
make build

# Run examples
make examples

# See all available commands
make help

License

MIT License

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

cobli_logging_formatter-1.0.0.tar.gz (91.8 kB view details)

Uploaded Source

Built Distribution

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

cobli_logging_formatter-1.0.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file cobli_logging_formatter-1.0.0.tar.gz.

File metadata

  • Download URL: cobli_logging_formatter-1.0.0.tar.gz
  • Upload date:
  • Size: 91.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for cobli_logging_formatter-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8bffa4c25c86b1c7dca11dba254da41408a9e73969505147f3b9c52255961ee6
MD5 be25384283922c7481909385c132e8f3
BLAKE2b-256 f3e0a6ca73c4ca454e62e8c520e2765e8237159e117c410c2008f201517811a5

See more details on using hashes here.

File details

Details for the file cobli_logging_formatter-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cobli_logging_formatter-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd39bc9db3b22eac0f8acc5e91b412e5a24facc8f3617d14f467c101efd2ef32
MD5 3f031a574f7a72c5e2db43cb87c2f488
BLAKE2b-256 eef0bfea0b4082150783d2ee7cad84ebf6ea32164b975b470b5fe1103c536cb0

See more details on using hashes here.

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