Skip to main content

Crate witchcraft_env_logger

Crate witchcraft_env_logger 

Source
Expand description

A simple Witchcraft logger that can be configured with an environment variable.

This is similar to the env_logger crate, but using the witchcraft_log crate instead of the log crate. Configuration of logging levels is the same as env_logger except for the additional fatal log level. Regex filters are not supported.

Logs are written to standard error in the standard Witchcraft service.1 JSON format.

§Example

use witchcraft_log::{debug, error, info, Level};

witchcraft_env_logger::init();

debug!("this is a debug message");
error!("this is printed by default");

if witchcraft_log::enabled!(Level::Info) {
    let x = 3 * 4; // expensive computation
    info!("figured out the answer", safe: { answer: x });
}
$ RUST_LOG=error ./main
{"type":"service.1","level":"ERROR","time":"2025-05-26T16:45:59.204677531Z","origin":"main","thread":"main","message":"this is printed by default","safe":true,"params":{"file":"witchcraft-env-logger/examples/main.rs","line":7}}
$ RUST_LOG=info ./main
{"type":"service.1","level":"ERROR","time":"2025-05-26T16:46:31.043928664Z","origin":"main","thread":"main","message":"this is printed by default","safe":true,"params":{"file":"witchcraft-env-logger/examples/main.rs","line":7}}
{"type":"service.1","level":"INFO","time":"2025-05-26T16:46:31.043976765Z","origin":"main","thread":"main","message":"figured out the answer","safe":true,"params":{"answer":12,"file":"witchcraft-env-logger/examples/main.rs","line":11}}
$ RUST_LOG=main=debug ./main
{"type":"service.1","level":"DEBUG","time":"2025-05-26T16:47:04.831643644Z","origin":"main","thread":"main","message":"this is a debug message","safe":true,"params":{"file":"witchcraft-env-logger/examples/main.rs","line":6}}
{"type":"service.1","level":"ERROR","time":"2025-05-26T16:47:04.831691314Z","origin":"main","thread":"main","message":"this is printed by default","safe":true,"params":{"file":"witchcraft-env-logger/examples/main.rs","line":7}}
{"type":"service.1","level":"INFO","time":"2025-05-26T16:47:04.831720469Z","origin":"main","thread":"main","message":"figured out the answer","safe":true,"params":{"answer":12,"file":"witchcraft-env-logger/examples/main.rs","line":11}}

Functions§

init
Like try_init(), but panics if the logger is already initialized.
try_init
Initializes the global logger, reading configuration from the RUST_LOG environment variable.