Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a minimal example #5

Open
ufoscout opened this issue Aug 28, 2019 · 1 comment
Open

Provide a minimal example #5

ufoscout opened this issue Aug 28, 2019 · 1 comment

Comments

@ufoscout
Copy link
Contributor

The signal.rs example is difficult to understand as it contains lots of code not related to slog_atomic.

I would propose to add a simpler example that focuses only on the ability of slog_atomic to alter the logger configuration at runtime.

For example, I used this piece of code to show it to my collegues:

use slog::*;
use slog_async::Async;

fn main() {
    let debug_drain = new_drain(Level::Debug);
    let info_drain = new_drain(Level::Info);
    let warn_drain = new_drain(Level::Warning);

    // init AtomicSwitch
    let drain = slog_atomic::AtomicSwitch::new(debug_drain);
    let log_ctrl = drain.ctrl();

    // Init root logger
    // Here the global log level is set to DEBUG
    let log = slog::Logger::root(drain, o!());

    println!("------------------");
    debug!(log, "debug log 1"); // this is logged
    info!(log, "info log 1"); // this is logged
    warn!(log, "warn log 1"); // this is logged

    // Change the log level at runtime.
    // Now the global log level is set to INFO
    log_ctrl.set(info_drain);

    println!("------------------");
    debug!(log, "debug log 2"); // this is NOT logged anymore
    info!(log, "info log 2"); // this is logged
    warn!(log, "warn log 2"); // this is logged

    // Change the log level at runtime.
    // Now the global log level is set to WARN
    log_ctrl.set(warn_drain);

    println!("------------------");
    debug!(log, "debug log 3"); // this is NOT logged anymore
    info!(log, "info log 3"); // this is NOT logged anymore
    warn!(log, "warn log 3"); // this is logged
}

fn new_drain(level: Level) -> Fuse<LevelFilter<Fuse<Async>>> {
    let decorator = slog_term::TermDecorator::new().build();
    let drain = slog_term::FullFormat::new(decorator).build().fuse();
    let drain = slog_async::Async::new(drain).build().fuse();
    drain.filter_level(level).fuse()
}
@dpc
Copy link
Contributor

dpc commented Aug 28, 2019

Can you put it in a PR that adds it to ./examples/? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants