Crates.io | kvlogger |
lib.rs | kvlogger |
version | 0.5.0 |
source | src |
created_at | 2020-03-11 22:56:03.950987 |
updated_at | 2021-02-04 17:12:16.560086 |
description | A simple human readable key-value logger |
homepage | |
repository | https://github.com/apognu/kvlogger |
max_upload_size | |
id | 217683 |
size | 44,221 |
Like other log
gers, kvlogger
must be initialized and registered as the default log handler in your program. You can then use either that usual log macros (that do not handle key/value pairs), or use the kvlog!
macro and add your data.
Any type that implements Display
can be used as a value.
use std::error::Error;
use log::*;
use kvlogger::{KvLoggerBuilder, *};
fn main() -> Result<(), Box<dyn Error>> {
KvLoggerBuilder::default()
.set_level(Level::Debug)
.set_datetime_format("%Y-%m-%d")
.init()?;
info!("a simple message");
kvlog!(Info, "user tried to log in", {
"username" => "apognu",
"status" => 200
});
Ok(())
}
kvlogger
uses env_logger under the hood for filter selection. You have two ways to configure the desired level for your logs:
set_level(mut self, log::Level)
method of KvLoggerBuilder
:KvLoggerBuilder::default()
.set_level(Level::Debug)
.init()?;
RUST_LOG
environment variable to specify which logs should be considered. See the env_logger documentation for more information.$ RUST_LOG=rocket=error,main=info cargo run
datetime
feature (see below), you can specify another format from the default, with the set_datetime_format(mut self, Into<String>)
method:KvLoggerBuilder::default()
.set_datetime_format("%Y-%m-%d")
.init()?;
By default, the date used is printed as the number of milliseconds since UNIX epoch. You can opt in the use of more complex (and human readable) formats by enabling the datetime
feature in Cargo.toml
:
[dependencies]
kvlogger = { version = "*", features = ["datetime"] }
$ cargo run --example simple
$ cargo run --example simple --features datetime
$ RUST_LOG=simple=trace cargo run --example simple --features datetime