Crate slog_scope

Source
Expand description

Logging scopes for slog-rs

Logging scopes are convenience functionality for slog-rs to free user from manually passing Logger objects around.

Set of macros is also provided as an alternative to original slog crate macros, for logging directly to Logger of the current logging scope.

§Set global logger upfront

Warning: Since slog-scope version 4.0.0, slog-scope defaults to panicking on logging if no scope or global logger was set. Because of it, it is advised to always set a global logger upfront with set_global_logger.

§Using slog-scope as a part of API is not advised

Part of a slog logging philosophy is ability to freely express logging contexts according to logical structure, rather than callstack structure. By using logging scopes the logging context is tied to code flow again, which is less expressive.

It is generally advised NOT to use slog_scope in libraries. Read more in slog-rs FAQ

#[macro_use(slog_o, slog_info, slog_log, slog_record, slog_record_static, slog_b, slog_kv)]
extern crate slog;
#[macro_use]
extern crate slog_scope;
extern crate slog_term;

use slog::Drain;

fn foo() {
    slog_info!(slog_scope::logger(), "foo");
    info!("foo"); // Same as above, but more ergonomic and a bit faster
                  // since it uses `with_logger`
}

fn main() {
    let plain = slog_term::PlainSyncDecorator::new(std::io::stdout());
    let log = slog::Logger::root(
        slog_term::FullFormat::new(plain)
        .build().fuse(), slog_o!()
    );

    // Make sure to save the guard, see documentation for more information
    let _guard = slog_scope::set_global_logger(log);
    slog_scope::scope(&slog_scope::logger().new(slog_o!("scope" => "1")),
        || foo()
    );
}

Macros§

  • Log a critical level message using current scope logger
  • Log a debug level message using current scope logger
  • Log a error level message using current scope logger
  • Log a info level message using current scope logger
  • Log critical level record (alias)
  • Log debug level record (alias)
  • Log error level record
  • Log info level record (alias)
  • Log trace level record (alias)
  • Log warning level record (alias)
  • Log a trace level message using current scope logger
  • Log a warning level message using current scope logger

Structs§

Functions§

  • Access the Logger for the current logging scope
  • Execute code in a logging scope
  • Set global Logger that is returned by calls like logger() outside of any logging scope.
  • Access the Logger for the current logging scope