Crate slog_scope[][src]

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

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

slog_crit

Log critical level record (alias)

slog_debug

Log debug level record (alias)

slog_error

Log error level record

slog_info

Log info level record (alias)

slog_trace

Log trace level record (alias)

slog_warn

Log warning level record (alias)

trace

Log a trace level message using current scope logger

warn

Log a warning level message using current scope logger

Structs

GlobalLoggerGuard

Guard resetting global 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.

with_logger

Access the Logger for the current logging scope