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.
- Request ID Propagation: Automatically injects a
request-idinto all log messages. - FastAPI Integration: Middleware for FastAPI to handle
request-idfor incoming HTTP requests. - Celery Integration: Signal handlers to propagate the
request-idto Celery tasks. - Requests Library Patching: Automatically injects the
request-idinto outgoing HTTP requests made with therequestslibrary.
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 setup_logging()
-
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 signal handlers to ensure they are registered. You don't need to call them directly.
from litewave_logger.celery import propagate_request_id_to_celery, clear_request_id_after_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
# These imports are needed to register the signal handlers
from litewave_logger.celery import propagate_request_id_to_celery, clear_request_id_after_celery
# 1. Initialize logging
setup_logging()
# 2. Patch requests library
patch_requests()
app = FastAPI()
# 3. Add RequestIdMiddleware
app.add_middleware(RequestIdMiddleware)
# Your application code here...
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.1.0.tar.gz.
File metadata
- Download URL: litewave_logger-0.1.0.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ed16f6abbb1ac5830a28e44922e13b442eb5bd0dea0c0999d47a888a351bc9c
|
|
| MD5 |
d7ef35ba010019ed2a3ad385d066c4a9
|
|
| BLAKE2b-256 |
ec1d61647dd216eb1803ae63d83d576a09bcc52e3f8a9d3cc278a98e5309725b
|
File details
Details for the file litewave_logger-0.1.0-py3-none-any.whl.
File metadata
- Download URL: litewave_logger-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f184851737196f8b5bb2ae77deffa1879d48dcdc4127a8cc494ee92d69502f14
|
|
| MD5 |
2384e5d02da908bc4f0f5dd5f069ec32
|
|
| BLAKE2b-256 |
43b378a938609cf9ae53fb3a5d293156f0db7292705548084d8582fc4cecf607
|