structured-logger

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
Log panics example: https://github.com/iorust/structured-logger/blob/main/examples/panic_log.rs
Simple example:
use serde::Serialize;
use std::{fs::File, io::stdout};
use structured_logger::{json::new_json_writer, unix_ms, Logger};
fn main() {
let log_file = File::options()
.create(true)
.append(true)
.open("app.log")
.unwrap();
Logger::new()
.with_target_writer("api", new_json_writer(stdout()))
.with_target_writer("file", new_json_writer(log_file))
.init();
let kv = ContextLog {
uid: "user123".to_string(),
action: "upate_book".to_string(),
};
log::info!("hello world");
log::info!(target: "api",
method = "GET",
path = "/hello",
status = 200_u16,
start = unix_ms(),
elapsed = 10_u64,
kv = log::as_serde!(kv);
"",
);
log::info!(target: "file",
method = "GET",
path = "/hello",
status = 200_u16,
start = unix_ms(),
elapsed = 10_u64,
kv = log::as_serde!(kv);
"",
);
}
#[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.