[][src]Function logsley::configure

pub fn configure(filters: &str) -> Result<GlobalLoggerGuard, Box<dyn Error>>

Configure log and slog to emit JSON to stdout.

Applies global and per-module filtering rules from filters and overrides them with rules from the RUST_LOG environment variable. The filters syntax is quite expressive. See the slog_envlogger docs

You can set the DEV_LOG_FORMAT environment variable to one of:

  • "json" or "" to log in JSON format.
  • "compact" to print scope variables on their own line and indent log messages emitted inside the scope.
  • "full" to print scope variables on every line, with color.
  • "plain" to print without color.

Release binaries always log JSON. Only debug binaries check the DEV_LOG_FORMAT env var.

Examples:

  • Set the default log level to info. The program will emit log messages with level info and higher.
    let _global_logger_guard = logsley::configure("info");
    logsley::info!("a message"; "some_data" => 123, "other_data" => "val1");
    slog::info!(slog_scope::logger(), "a message"; "some_data" => 123, "other_data" => "val1");
    log::info!("a message; some_data={} other_data={}", 123, "val1");
    log::debug!("some details");  // Not emitted
  • Set the default log level to info and set the level for chatty::module1 to warn.
    let _global_logger_guard = logsley::configure("info,chatty::module1=warn");
  • Use the environment variable to override default log level. module1 still gets its special log level.
    std::env::set_var("RUST_LOG", "debug");
    let _global_logger_guard = logsley::configure("info,module1=warn");
  • Use the environment variable to set module1 to debug.
    std::env::set_var("RUST_LOG", "module1=debug");
    let _global_logger_guard = logsley::configure("info");

Example output:

{"time_ns":1585851354242507000, "time":"2020-04-02T18:15:54.242521000Z", \
"module":"mod1","level":"ERROR","message":"msg1", "thread":"main","x":2}