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.
  • JSON Logging: All logs are formatted as JSON for easy parsing and integration with log aggregation systems.
  • 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 and adds it to response headers.
  • Celery Integration: Signal handlers to propagate the request-id to Celery tasks automatically.
  • Requests Library Patching: Automatically injects the request-id into outgoing HTTP requests made with the requests library.
  • Endpoint Exclusion: Configure endpoints that should not be logged (e.g., health checks, metrics).

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
    
    # Optionally exclude endpoints from logging (e.g., health checks, metrics)
    setup_logging(excluded_endpoints=['/health', '/metrics'])
    
  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 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

  1. Incoming HTTP Request: The RequestIdMiddleware checks for an X-Request-ID header. If present, it uses that value; otherwise, it generates a new UUID.
  2. Context Variable: The request ID is stored in a context variable (request_id_var) that is automatically maintained across async operations.
  3. Logging: All log messages automatically include the request ID via the RequestIdFilter.
  4. Outgoing Requests: When using the requests library, the request ID is automatically injected as the X-Request-ID header.
  5. Response Headers: The request ID is added to the response headers as X-Request-ID.
  6. 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


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.4.0.tar.gz (6.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.4.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: litewave_logger-0.4.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for litewave_logger-0.4.0.tar.gz
Algorithm Hash digest
SHA256 ab8d3716c0cba144420cdfda22737e284093152d38e9028a8b15ffd6fbe20da6
MD5 4cafd29948a3c8d2b0da94e368de3055
BLAKE2b-256 00c97eed4401bae88382db363a52bfa30a7fe95900a781c4d6e7a734fb7def4a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for litewave_logger-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 01448e7035e8b125b82b4fccf5732e414ac708c8d321ec387dba9c0ef6d74d92
MD5 579090808af27b808ad49b34a8591723
BLAKE2b-256 7f688d633038a1eace7e345f73b162a2e62db88441b793915931238b8089e993

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