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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cobli_logging_formatter-1.0.1.tar.gz.
File metadata
- Download URL: cobli_logging_formatter-1.0.1.tar.gz
- Upload date:
- Size: 86.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
092d1578c68e6665c397190efaddca680fd74028b65c218d51614f69a1024e12
|
|
| MD5 |
b0e1f79accf79fd8f9247993c97bc9f4
|
|
| BLAKE2b-256 |
fcd65a5c74ef6c22b39ec62f3c7edbbaa2204ce9e0044e239f1c201b5c6a685b
|
File details
Details for the file cobli_logging_formatter-1.0.1-py3-none-any.whl.
File metadata
- Download URL: cobli_logging_formatter-1.0.1-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7928bf173c0f1ca98a725bf33d2962e37e043e9138ba3f57951bcad65f68f414
|
|
| MD5 |
60655b387a84b8af616009b6ab056e24
|
|
| BLAKE2b-256 |
af1b1d2a8962d52b34f805e4969fab9eca431eb3cfe2428cea312d07f34f407b
|