Crate structured_logger
source ·Expand description
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.
This crate provides only a logging implementation. To do actual logging use
the log
crate and it’s various macros.
Setting severity
You can use various environment variables to change the severity (log level) of the messages to actually log and which to ignore.
LOG
and LOG_LEVEL
can be used to set the severity to a specific value,
see the log
’s package LevelFilter
type for available values.
## In your shell of your choice:
## Set the log severity to only print log messages with info severity or
## higher, trace and debug messages won't be printed anymore.
$ LOG=info ./my_binary
## Set the log severity to only print log messages with warning severity or
## higher, informational (or lower severity) messages won't be printed
## anymore.
$ LOG=warn ./my_binary
Alternatively setting the TRACE
variable (e.g. TRACE=1
) sets the
severity to the trace, meaning it will log everything. Setting DEBUG
will
set the severity to debug.
## In your shell of your choice:
## Enables trace logging.
$ TRACE=1 ./my_binary
## Enables debug logging.
$ DEBUG=1 ./my_binary
If none of these environment variables are found it will default to an information severity.
Crate features
This crate has three features:
- log-panic, enabled by default.
Log-panic feature
The log-panic feature will log all panics using the error
severity,
rather then using the default panic handler. It will log the panic message
as well as the location and a backtrace, see the log output for an
panic_log
example.
Examples
use serde::Serialize;
use std::io::stdout;
use structured_logger::{json::new_json_writer, unix_ms, Logger};
fn main() {
// Initialize the logger.
Logger::new()
// set a specific writer (format to JSON, write to stdout) for target "request".
.with_target_writer("request", new_json_writer(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,
}
Modules
Structs
- A key in a structured key-value pair.
- A struct that holds the configuration for the logger.
- A value in a structured key-value pair.
Traits
- A trait that defines how to write a log.
Functions
- Returns the log level from the environment variables.
- Returns the current unix timestamp in milliseconds.
Type Definitions
- A type alias for BTreeMap<Key<’a>, Value<’a>>. BTreeMap is used to keep the order of the keys.