Skip to main content

A Python logging handler for Google Cloud Logging with request tracing support

Project description

Cloud Logging Handler

PyPI version Python Versions License: MIT

A Python logging handler for Google Cloud Logging with request tracing support.

Features

  • Structured JSON Logging: Outputs logs in Google Cloud Logging's structured format
  • Request Tracing: Automatic trace context propagation via X-Cloud-Trace-Context header
  • Log Aggregation: Aggregates all logs within a single request into one log entry
  • Severity Tracking: Automatically tracks the highest severity level per request
  • Custom JSON Encoder: Support for high-performance JSON libraries (e.g., ujson)
  • Zero Dependencies: Core handler has no external dependencies

Installation

# Using uv (recommended)
uv add cloud-logging-handler

# Using pip
pip install cloud-logging-handler

Quick Start

Basic Usage

import logging
from cloud_logging_handler import CloudLoggingHandler

# Create handler
handler = CloudLoggingHandler(
    trace_header_name="X-Cloud-Trace-Context",
    project="your-gcp-project-id"
)

# Configure logging
logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)

# Log messages
logger.info("Hello, Cloud Logging!")

FastAPI Integration

import logging
import os
from fastapi import FastAPI, Request
from cloud_logging_handler import CloudLoggingHandler, RequestLogs

app = FastAPI()

# Initialize handler
handler = CloudLoggingHandler(
    trace_header_name="X-Cloud-Trace-Context",
    project=os.environ.get("GCP_PROJECT"),
)

# Configure logging
logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)

# Add middleware for request context
@app.middleware("http")
async def logging_middleware(request: Request, call_next):
    request.state.token = handler.set_request(RequestLogs(request, None))
    response = await call_next(request)
    handler.flush()
    return response

@app.get("/")
async def root():
    logging.info("Processing request")
    return {"message": "Hello World"}

Using with ujson

For better JSON serialization performance:

import ujson
from cloud_logging_handler import CloudLoggingHandler

handler = CloudLoggingHandler(
    trace_header_name="X-Cloud-Trace-Context",
    json_impl=ujson,
    project="your-gcp-project-id"
)

Log Output Format

With Request Context

When logging within a request context, logs are aggregated and output as structured JSON:

{
  "severity": "INFO",
  "name": "root",
  "process": 12345,
  "url": "https://example.com/api/endpoint",
  "logging.googleapis.com/trace": "projects/your-project/traces/abc123",
  "logging.googleapis.com/spanId": "def456",
  "message": "\n2025-12-01T12:00:00.000000+00:00\tINFO\tProcessing request\n2025-12-01T12:00:00.001000+00:00\tINFO\tRequest completed"
}

Without Request Context

When logging outside a request context, logs are output as plain text:

Processing request

Configuration

CloudLoggingHandler Parameters

Parameter Type Description
trace_header_name str HTTP header name for trace context (e.g., X-Cloud-Trace-Context)
json_impl module Custom JSON encoder module (must have dumps method)
project str GCP project ID for trace URL construction

Environment Variables

Variable Description
GCP_PROJECT Google Cloud Project ID

How It Works

  1. Request Start: Middleware creates a RequestLogs context
  2. Log Accumulation: All log calls within the request are accumulated in message field
  3. Severity Tracking: The highest severity level is tracked
  4. Trace Extraction: Trace context is extracted from request headers
  5. Request End: flush() emits all accumulated logs as a single structured entry

This approach provides several benefits:

  • Correlate all logs from a single request
  • View logs grouped by trace in Cloud Console
  • Reduce log volume while maintaining detail

Development

Setup

# Clone the repository
git clone https://github.com/loplat/gcp-cloud-logging-handler.git
cd cloud-logging-handler

# Install with dev dependencies using uv
uv sync --all-extras

# Run tests
uv run pytest

# Run linting
uv run ruff check .
uv run ruff format .

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

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

cloud_logging_handler-0.2.0.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

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

cloud_logging_handler-0.2.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cloud_logging_handler-0.2.0.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for cloud_logging_handler-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a24e9161f6d7a0abecef053626ee61ca8e53140c905cc599624c7f702c5dc897
MD5 7d53b14f2a848ff3fb272654d1681e1f
BLAKE2b-256 b0fe015113bd561abb7901c3a43aee90235fc30cbfb46b39a11ffed119402557

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cloud_logging_handler-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1628a5f09601573b689497b0cc0b23e9044098fa615c033b5533f63b83a19fe2
MD5 d4b0888a8d4e17a91079df13e1cb6633
BLAKE2b-256 b9864e25d78743f639d570182f6409027667ab7c496e0f8b636c8a30b65524fb

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