Skip to main content

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.
  • Flask Integration: Middleware for Flask 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.
  • HTTPX Patching: Automatically injects the request-id into outgoing HTTP requests made with httpx (sync and async clients).
  • 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)
    

    Or for Flask applications: If your service is a Flask application, use the FlaskRequestIdMiddleware.

    from flask import Flask
    from litewave_logger.flask_middleware import FlaskRequestIdMiddleware
    
    app = Flask(__name__)
    FlaskRequestIdMiddleware(app)
    
  3. Patch the requests library: To ensure the request-id is propagated to other services via sync HTTP calls, patch the requests library.

    from litewave_logger.requests import patch_requests
    
    patch_requests()
    
  4. Patch httpx: To propagate the request-id via async HTTP calls (e.g. httpx.AsyncClient), patch httpx.

    from litewave_logger.httpx_patch import patch_httpx
    
    patch_httpx()
    
  5. 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
    

FastAPI 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
from litewave_logger.httpx_patch import patch_httpx

# 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 outgoing HTTP libraries
patch_requests()
patch_httpx()

app = FastAPI()

# 3. Add RequestIdMiddleware
app.add_middleware(RequestIdMiddleware)

# Your application code here...

Flask Example

Here's a complete example of how to integrate the litewave_logger into a Flask application:

from flask import Flask
from litewave_logger import setup_logging
from litewave_logger.flask_middleware import FlaskRequestIdMiddleware
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 = Flask(__name__)

# 3. Add FlaskRequestIdMiddleware
FlaskRequestIdMiddleware(app)

@app.route('/')
def hello():
    return 'Hello, World!'

# Your application code here...

The Flask middleware also supports the application factory pattern:

from flask import Flask
from litewave_logger import setup_logging
from litewave_logger.flask_middleware import FlaskRequestIdMiddleware

middleware = FlaskRequestIdMiddleware()

def create_app():
    setup_logging(excluded_endpoints=['/health'])
    app = Flask(__name__)
    middleware.init_app(app)
    return app

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 or httpx 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. While the task runs, every log line also includes the Celery task_id as celery_task_id.

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,
  "celery_task_id": null
}

Metadata

Release files for litewave-logger 0.9.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for litewave-logger 0.9.4
File Size Uploaded
litewave_logger-0.9.4.tar.gz 20.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for litewave-logger 0.9.4
File Interpreter ABI Platform
litewave_logger-0.9.4-py3-none-any.whl Python 3 none any Details

Total release size: 36.2 kB

Release files / litewave_logger-0.9.4.tar.gz

Download URL litewave_logger-0.9.4.tar.gz
Size 20.7 kB
Tags Source
SHA-256 checksum
How to use checksums
cd5e0588f1a50db418953a2c0b5a8d284e2117d37cceebb633f05d16b4916a0c
BLAKE2b-256 checksum
How to use checksums
77682082d21efac966fba753a3947156af9cf96f13978408270be9eb5d6d22f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / litewave_logger-0.9.4-py3-none-any.whl

Download URL litewave_logger-0.9.4-py3-none-any.whl
Size 15.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
18e6ddfe3ff771d19db58b2b060ca3c5cefb0eb0cffe7f55e15ce282b5ea67d4
BLAKE2b-256 checksum
How to use checksums
34bc360f98758c160166327c8991517deea3fa9639770dd33c6185f3b0dc0196
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

0.9.4 This release

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page