structured-logger  

CI License Latest Version Crates.io

A logging implementation for the log crate that logs structured values as JSON (CBOR, or any other) into a file, stderr, stdout, or any other. Inspired by std-logger.

Usage

See the [API documentation] for more.

Example

```rust use serde::Serialize; use std::io::stdout; use structuredlogger::{json::newjsonwriter, unixms, Logger};

fn main() { // Initialize the logger. Logger::new() // set a specific writer (format to JSON, write to stdout) for target "request". .withtargetwriter("request", newjsonwriter(stdout())) .init();

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

log::info!("hello world");
// {"level":"INFO","message":"hello world","target":"simple","timestamp":1679655670735}

// mock request data
log::info!(target: "request",
    method = "GET",
    path = "/hello",
    status = 200_u16,
    start = unix_ms(),
    elapsed = 10_u64,
    kv = log::as_serde!(kv);
    "",
);
// {"elapsed":10,"kv":{"uid":"user123","action":"upate_book"},"level":"INFO","message":"","method":"GET","path":"/hello","start":1679655670735,"status":200,"target":"request","timestamp":1679655670735}

}

[derive(Serialize)]

struct ContextLog { uid: String, action: String, } ```

License

Copyright © 2023-present IO Rust.

ldclabs/cose is licensed under the MIT License. See LICENSE for the full license text.