Crate slog_scope [] [src]

Logging scopes for slog-rs

Logging scopes are convinience 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.

Note: Part of a slog logging philosophy is ability to freely express logging contexts according to logical structure, rather than code 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(o, slog_info, slog_log)]
extern crate slog;
#[macro_use]
extern crate slog_scope;
extern crate slog_term;

use slog::DrainExt;

fn foo() {
    slog_info!(slog_scope::logger(), "foo");
    info!("foo"); // Same as above, but more ergonomic.
}

fn main() {
    let log = slog::Logger::root(slog_term::streamer().stderr().build().fuse(), o!("version" => "0.5"));

    slog_scope::set_global_logger(log);
    slog_scope::scope(slog_scope::logger().new(o!("scope" => "1")),
        || foo()
    );
}

Macros

crit

Log a critical level message using current scope logger

debug

Log a debug level message using current scope logger

error

Log a error level message using current scope logger

info

Log a info level message using current scope logger

trace

Log a trace level message using current scope logger

warn

Log a warning level message using current scope logger

Functions

logger

Access the Logger for the current logging scope

scope

Execute code in a logging scope

set_global_logger

Set global Logger that is returned by calls like logger() outside of any logging scope.