A lightweight logging library with context carry forward
Project description
Litewave Logger
This module provides a centralized and consistent logging solution for Litewave services. It ensures that a request-id is maintained across all services, including HTTP requests and Celery tasks, allowing for easy tracing of requests as they propagate through the system.
Features
- Centralized Logging: A single module to configure and manage logging across all services.
- JSON Logging: All logs are formatted as JSON for easy parsing and integration with log aggregation systems.
- Request ID Propagation: Automatically injects a
request-idinto all log messages. - FastAPI Integration: Middleware for FastAPI to handle
request-idfor incoming HTTP requests and adds it to response headers. - Celery Integration: Signal handlers to propagate the
request-idto Celery tasks automatically. - Requests Library Patching: Automatically injects the
request-idinto outgoing HTTP requests made with therequestslibrary. - Endpoint Exclusion: Configure endpoints that should not be logged (e.g., health checks, metrics).
Installation
- Add the
litewave_loggerdirectory to your Python project. - Ensure that the dependencies listed in the main
requirements.txt(fastapi,celery,requests) are installed.
Usage
To use the litewave_logger in your service, follow these steps:
-
Initialize the logger: In your main application file (e.g.,
api.py), import and call thesetup_loggingfunction. This should be done as early as possible.from litewave_logger import setup_logging # Optionally exclude endpoints from logging (e.g., health checks, metrics) setup_logging(excluded_endpoints=['/health', '/metrics'])
-
Add the FastAPI middleware: If your service is a FastAPI application, add the
RequestIdMiddlewareto your FastAPI app.from fastapi import FastAPI from litewave_logger.middleware import RequestIdMiddleware app = FastAPI() app.add_middleware(RequestIdMiddleware)
-
Patch the
requestslibrary: To ensure therequest-idis propagated to other services, patch therequestslibrary.from litewave_logger.requests import patch_requests patch_requests()
-
Connect Celery signals: If your service uses Celery, you need to import the Celery module to ensure the signal handlers are registered. The signal handlers are automatically connected via decorators, so you don't need to call them directly.
# Just import the module - signal handlers are automatically registered import litewave_logger.celery
Example
Here's a complete example of how to integrate the litewave_logger into a FastAPI application:
from fastapi import FastAPI
from litewave_logger import setup_logging
from litewave_logger.middleware import RequestIdMiddleware
from litewave_logger.requests import patch_requests
# Import Celery module to register signal handlers (if using Celery)
import litewave_logger.celery
# 1. Initialize logging (optionally exclude endpoints)
setup_logging(excluded_endpoints=['/health', '/metrics'])
# 2. Patch requests library
patch_requests()
app = FastAPI()
# 3. Add RequestIdMiddleware
app.add_middleware(RequestIdMiddleware)
# Your application code here...
How It Works
Request ID Flow
- Incoming HTTP Request: The
RequestIdMiddlewarechecks for anX-Request-IDheader. If present, it uses that value; otherwise, it generates a new UUID. - Context Variable: The request ID is stored in a context variable (
request_id_var) that is automatically maintained across async operations. - Logging: All log messages automatically include the request ID via the
RequestIdFilter. - Outgoing Requests: When using the
requestslibrary, the request ID is automatically injected as theX-Request-IDheader. - Response Headers: The request ID is added to the response headers as
X-Request-ID. - Celery Tasks: When a Celery task is published, the request ID is automatically included in the task headers and propagated to the worker process.
Log Format
All logs are formatted as JSON with the following structure:
{
"timestamp": "2024-01-01 12:00:00",
"level": "INFO",
"request_id": "550e8400-e29b-41d4-a716-446655440000",
"path": "/api/users",
"method": "GET",
"message": "request received",
"status_code": 200,
"error": null
}
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 litewave_logger-0.5.0.tar.gz.
File metadata
- Download URL: litewave_logger-0.5.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2fe6c4a268c71406707c5c99deb33e43a8f5860882d08469b80065c01e5441c1
|
|
| MD5 |
e9357e10a62accfc815637c95083f534
|
|
| BLAKE2b-256 |
d96bb7a9a2853d5129db74422bd63651b95e28ab582d2bbe501673f7cf45200a
|
File details
Details for the file litewave_logger-0.5.0-py3-none-any.whl.
File metadata
- Download URL: litewave_logger-0.5.0-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
084bcf76d4c73cad8b3e0732442d124ec91e6540897549362287cfd36f10aa97
|
|
| MD5 |
cfab5ec73ad983372a104e84c7b90f77
|
|
| BLAKE2b-256 |
4a692810e4e8185c3a889e744b9c2bd90acc3fb3825594b7fa1fef0bd1d579cc
|