A lightweight logging library for Content Lab projects with distributed tracing support
Project description
goldcast-cl-logger
A lightweight, flexible logging library for Content Lab projects with distributed tracing support.
Installation
# Install from GitHub Packages (recommended)
pip install goldcast-cl-logger
# Install with HTTP extras for traced requests
pip install goldcast-cl-logger[http]
# Add to requirements.txt
goldcast-cl-logger==0.2.8
# Add to pyproject.toml (Poetry)
[[tool.poetry.source]]
name = "github"
url = "https://ghcr.io/goldcast"
[tool.poetry.dependencies]
goldcast-cl-logger = { version = "^0.2.8", source = "github" }
Features
- Simple API: Easy to use with minimal configuration
- JSON Logging by Default: Structured JSON logging for better observability
- Distributed Tracing: Built-in support for trace ID propagation
- Sentry Integration: Compatible with Sentry's distributed tracing
- Environment Configuration: Control logging format via environment variables
- Extra Context: Add custom fields to log entries
- File Logging: Optional file output support
- Zero Dependencies: Uses only Python standard library (requests is optional)
- HTTP & SQS Propagation: Automatic trace ID propagation for external calls
Quick Start
from cl_logger import get_logger
# Get a logger instance
logger = get_logger(__name__)
# Basic logging (JSON by default)
logger.info("Application started")
logger.error("Something went wrong")
# With extra context
logger.info("User action", extra={"user_id": 123, "action": "login"})
# Exception logging with traceback
try:
risky_operation()
except Exception as e:
logger.exception("Operation failed", extra={"operation": "risky_operation"})
Distributed Tracing
Automatic Trace ID Propagation
The logger automatically includes trace IDs in all log entries when used with the provided middleware:
# Django middleware automatically creates/extracts trace IDs
# In settings.py:
MIDDLEWARE = [
# ...
'content_lab_backend.middleware.logging_middleware.RequestLoggingMiddleware',
# ...
]
Manual Trace Context
from cl_logger import TraceContext, get_trace_id
# Create a new trace context
with TraceContext() as ctx:
logger.info("Starting operation")
# trace_id is automatically included in logs
current_trace_id = get_trace_id()
HTTP Request Propagation
from cl_logger import http_utils
# Use traced HTTP client for automatic trace propagation
response = http_utils.get("https://api.example.com/data")
response = http_utils.post("https://api.example.com/users", json={"name": "John"})
# Or use a traced session
session = http_utils.TracedSession()
response = session.get("https://api.example.com/data")
SQS Message Propagation
import boto3
from cl_logger import sqs_utils
# Wrap SQS client for automatic trace propagation
sqs = boto3.client('sqs')
traced_sqs = sqs_utils.TracedSQSClient(sqs)
# Send message with trace ID
traced_sqs.send_message(
QueueUrl='https://sqs.region.amazonaws.com/account/queue',
MessageBody='Hello World'
)
# Process incoming SQS message with trace context
def handle_message(message):
logger.info("Processing message")
# trace_id from message is automatically set
sqs_utils.process_sqs_message(sqs_message, handle_message)
Configuration
Environment Variables
CL_JSON_LOGGING: Set to"false"to disable JSON logging (default:"true")
Programmatic Configuration
from cl_logger import CLLogger
# Create logger with specific settings
logger = CLLogger(
name="my_app",
level="DEBUG",
json_logging=True, # Default
log_to_file="app.log"
)
# Toggle to normal logging at runtime
logger.set_json_logging(False)
# Change log level
logger.set_level("WARNING")
Output Examples
JSON Logging (Default)
{
"timestamp": "2024-01-15T10:30:45.123Z",
"level": "INFO",
"logger": "my_app",
"message": "User action",
"module": "views",
"function": "login",
"line": 42,
"trace_id": "550e8400-e29b-41d4-a716-446655440000",
"user_id": 123,
"action": "login"
}
Normal Logging
2024-01-15 10:30:45,123 - my_app - INFO - [550e8400-e29b-41d4-a716-446655440000] - User action | user_id=123 | action=login
Django Integration
Settings Configuration
In your Django settings:
# settings.py
import os
from cl_logger import get_logger
# Logger for settings module
logger = get_logger(__name__)
# JSON logging is enabled by default
# To disable: export CL_JSON_LOGGING=false
# Middleware configuration
MIDDLEWARE = [
# ...
'content_lab_backend.middleware.logging_middleware.RequestLoggingMiddleware',
# ...
]
Usage in Views
# views.py
from cl_logger import get_logger, get_trace_id
logger = get_logger(__name__)
def my_view(request):
# Trace ID is automatically available from middleware
logger.info("View accessed", extra={
"user_id": request.user.id,
"method": request.method,
"path": request.path
})
# Access trace ID if needed
trace_id = get_trace_id()
# or
trace_id = request.trace_id
Advanced Usage
Custom Logger Class
from cl_logger import CLLogger
class AppLogger(CLLogger):
def log_request(self, request_id, method, path, status, duration):
self.info("API Request", extra={
"request_id": request_id,
"method": method,
"path": path,
"status": status,
"duration_ms": duration
})
# Use custom logger
logger = AppLogger("api")
logger.log_request("abc123", "GET", "/api/users", 200, 45.2)
Adding Trace Metadata
from cl_logger import add_trace_metadata, get_trace_metadata
# Add metadata to current trace
add_trace_metadata("user_id", 123)
add_trace_metadata("tenant", "acme-corp")
# Metadata is automatically included in logs
logger.info("Processing request")
# Output includes: {..., "trace_metadata": {"user_id": 123, "tenant": "acme-corp"}}
Best Practices
-
Use Module Names: Always use
__name__for logger nameslogger = get_logger(__name__)
-
Structured Context: Use
extraparameter for structured datalogger.info("User action", extra={"user_id": 123, "action": "login"})
-
Consistent Field Names: Use consistent names for common fields
user_idfor user identifiersrequest_idortrace_idfor request trackingduration_msfor time measurementsstatusfor operation results
-
Let Middleware Handle Traces: The middleware automatically manages trace IDs for HTTP requests
-
Use Traced Clients for External Calls: Use the provided utilities for external calls to maintain trace continuity
Production Deployment
For production environments:
- JSON logging is enabled by default (recommended)
- Trace IDs are automatically propagated to Sentry
- Use structured logging for better searchability and alerting
- Configure your log aggregation service to parse JSON logs
Troubleshooting
Logs not appearing
- Check the log level - by default, DEBUG messages are not shown
- Ensure the logger is properly initialized:
logger = get_logger(__name__)
Trace IDs not propagating
- Ensure middleware is properly configured
- Use
http_utilsfor HTTP requests orsqs_utilsfor SQS messages - Check that trace context is active:
get_trace_id()should return a value
Performance considerations
- The logger is lightweight and adds minimal overhead
- Trace context uses Python's contextvars for efficient async support
- Extra fields are only processed when actually logging
License
MIT License - see LICENSE file for details.
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 goldcast_cl_logger-0.4.11.tar.gz.
File metadata
- Download URL: goldcast_cl_logger-0.4.11.tar.gz
- Upload date:
- Size: 12.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.13.2 Darwin/24.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7663bf4b93f1c3e79f3bc40bcd5bf86ab3962bf6579912b30f5b6bec6bc9a297
|
|
| MD5 |
dd1dc5816b995d0db68536f90b56213c
|
|
| BLAKE2b-256 |
34341f2d97ca27d253bb9fd485da0c8ec3d37604a5369f9d5fd3288ec681f6d7
|
File details
Details for the file goldcast_cl_logger-0.4.11-py3-none-any.whl.
File metadata
- Download URL: goldcast_cl_logger-0.4.11-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.13.2 Darwin/24.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
94ec220da8d5983ce86f9f6e845bb18b9e1cd6b01fc3de87ddd4245615cd49c9
|
|
| MD5 |
db24d97085600d2b248b6fa90e7943b5
|
|
| BLAKE2b-256 |
959c3da92e75df5c7270d0857b846b67f6bc2103dd79e7f11617587b14eb4972
|