Skip to main content

Crate wide_log

Crate wide_log 

Source
Expand description

§wide-log

A high-speed wide-event logging system for Rust. A single structured event accumulates fields throughout a request/task lifecycle and is emitted as one JSON line on completion.

§Quick Start

The wide_log! macro takes a JSON object literal and generates the key enum, Key trait impl, thread-local storage, guard builder, current() accessor, scope() / scope_default() async functions (behind the tokio feature), WideLogLayer tower middleware (behind the tokio feature), and all logging macros (wl_set!, wl_inc!, info!, etc.) in one invocation.

use wide_log::wide_log;

wide_log!({
    "service": {
        "name": null,
        "version": "1.0.0",
    },
    "requests": counter!,
});

fn main() {
    tracing_subscriber::fmt().init();
    let _guard = WideLogGuard::builder().build();
    wl_set!("service.name", "example-service");
    wl_inc!("requests");
    info!("request received");
    // _guard drops → duration.total_ms set, timestamp set,
    // event emitted as JSON.
}

§Auto-Added Keys

  • "log" — log entries from info!(), warn!(), etc. Handled internally; never declared by the user.
  • "duration" — elapsed time in ms. Auto-added as "duration": { "total_ms": duration! } if not declared.
  • "event" — event metadata. Auto-added as "event": { "timestamp": null, "id": null } if not declared. The timestamp is set to an RFC 3339 string on drop. The id is set to a ULID string (or UUIDv4 with the uuid feature) on build().

§Builder Pattern

Use WideLogGuard::builder() to construct a guard. The builder allows specifying a custom timezone, ID generator, and emit function:

// Default: UTC timezone, ULID ID, tracing emit
let _guard = WideLogGuard::builder().build();

// Custom timezone
use chrono_tz::Tz;
let _guard = WideLogGuard::builder()
    .with_timezone(Tz::America__New_York)
    .build();

// Custom ID generator
let _guard = WideLogGuard::builder()
    .with_id(|| "my-custom-id".to_string())
    .build();

// UUIDv4 ID (requires `uuid` feature)
let _guard = WideLogGuard::builder()
    .with_uuid()
    .build();

// Custom emit function
let _guard = WideLogGuard::builder()
    .with_emit(|ev| { println!("{}", ev.to_json().unwrap()); })
    .build();

§info! Shadowing

The generated info!, warn!, error!, debug!, trace! macros shadow tracing::info! etc. when both are in scope. To call the real tracing macros, use the fully qualified path: ::tracing::info!(...).

§Features

  • tokio — enables async support: scope(), scope_default(), WideLogLayer tower middleware, and tokio::task_local! storage.
  • uuid — enables WideLogGuardBuilder::with_uuid() for UUIDv4 ID generation instead of the default ULID.

Re-exports§

pub use error::Error;
pub use guard::ScopedGuard;
pub use key::Key;
pub use value::Value;
pub use wide_event::WideEvent;
pub use context::ContextCell;

Modules§

__re_exports_core
context
error
guard
key
value
wide_event

Macros§

wide_log
The wide_log! proc-macro. See the crate-level documentation for syntax and usage details.