slog 0.4.0

Structured, composable logging for Rust

slog-rs

Code snippet

fn main() {
    // Create a new group of loggers, sharing one drain.
    let root = Logger::new_root(v!("version" => VERSION, "build-id" => "8dfljdf"));

    // Create child loggers from existing ones. Children
    // clone `key: value` pairs from their parents.
    //
    // Build logging context as data becomes available.
    let log = root.new(v!("child" => 1));

    // Closures can be used for values that change at runtime.
    // Data captured by the closure needs to be `Send+Sync`.
    let counter = Arc::new(AtomicUsize::new(0));
    let log = log.new(v!("counter" => {
        let counter = counter.clone();
        move |_ : &_| { counter.load(SeqCst)}
    }));

    log.info("before-fetch-add", s!()); // counter == 0
    counter.fetch_add(1, SeqCst);
    log.info("after-fetch-add", s!()); // counter == 1

    // Drains can be swapped atomically (race-free).
    log.set_drain(
        // drains are composable
        drain::filter_level(
            Level::Info,
            drain::stream(
                std::io::stderr(),
                // multiple outputs formats are supported
                format::Json::new(),
                ),
            ),
        );

    // Closures can be used for lazy evaluation:
    // This `slow_fib` won't be evaluated, as the current drain discards
    // "trace" level logging records.
    log.debug("debug", s!("lazy-closure" => |_ : &_| slow_fib(40)));

    // Loggers are internally atomically reference counted so can be cloned,
    // passed between threads and stored without hassle.
    let join = thread::spawn({
        let log = log.clone();
        move || {
            log.info("subthread", s!("stage" => "start"));
            thread::sleep(Duration::new(1, 0));
            log.info("subthread", s!("stage" => "end"));
        }
    });

    join.join().unwrap();
}

See examples/features.rs for full code.

Introduction

Structured, composable logging for Rust. Work in progress, but usable already.

Heavily inspired by log15 for Go, which I liked so much, that I want it in Rust too.

Read Documentation for details and features.

If you want to say hi, or need help use #dpc gitter.im.

To report a bug or ask for features use github issues.

Building & running

If you need to install Rust (come on, you should have done that long time ago!), use rustup.

In your project

In Cargo.toml:

[dependencies]
slog = "*"

In your main.rs:

#[macro_use]
extern crate slog;