#logging #log-structured #json-log #log #logger #format-file #log-format

structured-logger

A logging implementation for the log crate that logs structured values either synchronous or asynchronous, as JSON, CBOR, or any other format, into a file, stderr, stdout, or any other destination

16 releases (4 stable)

1.0.3 Aug 13, 2023
1.0.2 Jul 26, 2023
1.0.1 Jun 20, 2023
0.5.3 Apr 3, 2023
0.5.2 Mar 30, 2023

#153 in Debugging

Download history 1666/week @ 2024-07-27 1274/week @ 2024-08-03 1299/week @ 2024-08-10 1264/week @ 2024-08-17 1130/week @ 2024-08-24 1357/week @ 2024-08-31 1166/week @ 2024-09-07 987/week @ 2024-09-14 1094/week @ 2024-09-21 1127/week @ 2024-09-28 1368/week @ 2024-10-05 1615/week @ 2024-10-12 1431/week @ 2024-10-19 1388/week @ 2024-10-26 1305/week @ 2024-11-02 1095/week @ 2024-11-09

5,432 downloads per month
Used in 10 crates (9 directly)

MIT/Apache

27KB
399 lines

structured-logger

License Crates.io Codecov CI Docs.rs Latest Version

A logging implementation for the log crate that logs structured values either synchronous or asynchronous, in JSON, CBOR, or any other format, to a file, stderr, stdout, or any other destination.

It is inspired by std-logger.

Usage

See examples and the API documentation for more.

Example

Simple example:

use serde::Serialize;
use structured_logger::{async_json::new_writer, unix_ms, Builder};

#[tokio::main]
async fn main() {
    // Initialize the logger.
    Builder::with_level("info")
        .with_target_writer("*", new_writer(tokio::io::stdout()))
        .init();

    let kv = ContextLog {
        uid: "user123".to_string(),
        action: "upate_book".to_string(),
    };

    log::info!("hello world");
    // This log will be written to stdout:
    // {"level":"INFO","message":"hello world","target":"simple","timestamp":1679745592127}

    log::info!(target: "api",
        method = "GET",
        path = "/hello",
        status = 200_u16,
        start = unix_ms(),
        elapsed = 10_u64,
        kv = log::as_serde!(kv);
        "",
    );
    // This log will be written to stdout:
    // {"elapsed":10,"kv":{"uid":"user123","action":"upate_book"},"level":"INFO","message":"","method":"GET","path":"/hello","start":1679745592127,"status":200,"target":"api","timestamp":1679745592127}
}

#[derive(Serialize)]
struct ContextLog {
    uid: String,
    action: String,
}

License

Copyright © 2023-present IO Rust.

iorust/structured-logger is licensed under either of Apache License, Version 2.0 or MIT license at your option.

Dependencies

~3–9.5MB
~96K SLoC