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.2.0.tar.gz (5.1 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.2.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file litewave_logger-0.2.0.tar.gz.

File metadata

  • Download URL: litewave_logger-0.2.0.tar.gz
  • Upload date:
  • Size: 5.1 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.2.0.tar.gz
Algorithm Hash digest
SHA256 b891795eb7a0e111d0776100ab67b9629956069fb4705aa444291c2d81235d11
MD5 8408050cabc98e40535cf956b93e0875
BLAKE2b-256 34a46d6dc21da03a2bde00e90c676464a734f4a2fb1d0d0fc597a6a89dee9726

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for litewave_logger-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ebf9528f1043e3bf31240c6d9a3030071786f27c422c0a8f296f09d70e600ca2
MD5 06807f338c9bd7fddc7c16a94e5a1bff
BLAKE2b-256 11bd9ef91bdb98280a5c9a649809ca08929588f9b9bae8341e8a88520c7f740a

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