Skip to main content

Module logging

Module logging 

Source
Expand description

Structured logging infrastructure for fastapi_rust.

This module provides structured logging that integrates with asupersync’s observability system and automatically propagates request context.

§Design Principles

  1. Context propagation: Log macros auto-inject request_id, region_id, task_id
  2. Structured output: All logs are JSON-formatted for production
  3. Span-based timing: Instrument operations with hierarchical spans
  4. asupersync integration: Delegates to asupersync’s observability module
  5. Zero-allocation fast path: Critical paths avoid heap allocation

§Usage

§Basic Logging

use fastapi_core::logging::*;

async fn handler(ctx: &RequestContext) -> impl IntoResponse {
    log_info!(ctx, "Processing request");
    log_debug!(ctx, "Request path: {}", ctx.request().path());

    // With structured fields
    log_info!(ctx, "User authenticated",
        user_id => user.id,
        role => user.role
    );

    "ok"
}

§Timing Spans

use fastapi_core::logging::*;

async fn handler(ctx: &RequestContext) -> impl IntoResponse {
    let span = ctx.span("database_query");
    let result = db.query("SELECT ...").await?;
    span.end(); // Logs duration

    // Or with auto-end on drop
    {
        let _span = ctx.span_auto("serialize");
        serde_json::to_string(&result)?
    } // Span ends here
}

§JSON Output Schema

{
    "timestamp": "2024-01-17T10:30:45.123456789Z",
    "level": "info",
    "message": "User authenticated",
    "request_id": 12345,
    "region_id": 1,
    "task_id": 42,
    "target": "my_app::handlers::auth",
    "fields": {
        "user_id": 67890,
        "role": "admin"
    }
}

§Configuration

Logging is configured via LogConfig:

use fastapi_core::logging::{LogConfig, LogLevel};

let config = LogConfig::new()
    .level(LogLevel::Debug)          // Minimum level to emit
    .json_output(true)               // JSON format (false = compact)
    .include_target(true)            // Include module path
    .max_fields(16);                 // Max structured fields per log

let app = App::new()
    .with_logging(config);

Structs§

AutoSpan
Auto-ending span that logs duration on drop.
LogConfig
Configuration for the logging system.
LogEntry
A structured log entry with context.
RequestLogger
A per-request logger that captures context and emits logs.
Span
A timing span for instrumentation.

Enums§

LogLevel
Log levels matching asupersync’s observability module.

Functions§

global_log_level
Returns the current global log level.
level_enabled
Returns true if the given level is enabled.
set_global_log_level
Sets the global log level.