Skip to main content

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-id into all log messages.
  • FastAPI Integration: Middleware for FastAPI to handle request-id for incoming HTTP requests.
  • Celery Integration: Signal handlers to propagate the request-id to Celery tasks.
  • Requests Library Patching: Automatically injects the request-id into outgoing HTTP requests made with the requests library.

Installation

  1. Add the litewave_logger directory to your Python project.
  2. 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:

  1. Initialize the logger: In your main application file (e.g., api.py), import and call the setup_logging function. This should be done as early as possible.

    from litewave_logger import setup_logging
    
    setup_logging()
    
  2. Add the FastAPI middleware: If your service is a FastAPI application, add the RequestIdMiddleware to your FastAPI app.

    from fastapi import FastAPI
    from litewave_logger.middleware import RequestIdMiddleware
    
    app = FastAPI()
    app.add_middleware(RequestIdMiddleware)
    
  3. Patch the requests library: To ensure the request-id is propagated to other services, patch the requests library.

    from litewave_logger.requests import patch_requests
    
    patch_requests()
    
  4. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

litewave_logger-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

litewave_logger-0.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

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

Hashes for litewave_logger-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4ed16f6abbb1ac5830a28e44922e13b442eb5bd0dea0c0999d47a888a351bc9c
MD5 d7ef35ba010019ed2a3ad385d066c4a9
BLAKE2b-256 ec1d61647dd216eb1803ae63d83d576a09bcc52e3f8a9d3cc278a98e5309725b

See more details on using hashes here.

File details

Details for the file litewave_logger-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for litewave_logger-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f184851737196f8b5bb2ae77deffa1879d48dcdc4127a8cc494ee92d69502f14
MD5 2384e5d02da908bc4f0f5dd5f069ec32
BLAKE2b-256 43b378a938609cf9ae53fb3a5d293156f0db7292705548084d8582fc4cecf607

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