42 releases (stable)

2.9.1 Feb 18, 2024
2.9.0 Feb 21, 2022
2.8.0 Feb 10, 2021
2.6.0 May 29, 2020
0.6.0 Jul 19, 2016

#22 in Unix APIs

Download history 31221/week @ 2023-12-22 41568/week @ 2023-12-29 66154/week @ 2024-01-05 60800/week @ 2024-01-12 69706/week @ 2024-01-19 71083/week @ 2024-01-26 72464/week @ 2024-02-02 69142/week @ 2024-02-09 69533/week @ 2024-02-16 77483/week @ 2024-02-23 78545/week @ 2024-03-01 77264/week @ 2024-03-08 78934/week @ 2024-03-15 77367/week @ 2024-03-22 72985/week @ 2024-03-29 63121/week @ 2024-04-05

306,163 downloads per month
Used in 298 crates (186 directly)

MPL-2.0 OR MIT OR Apache-2.0

51KB
1K SLoC

slog-rs logo
Travis CI Build Status slog-term on crates.io slog-rs Gitter Chat

slog-term - Terminal output drain for slog-rs

For more information, help, to report issues etc. see slog-rs.


lib.rs:

slog-rs's Drain for terminal output

This crate implements output formatting targeting logging to terminal/console/shell or similar text-based IO.

Warning: slog-term (like slog-rs itself) is fast, modular and extensible. It comes with a price: a lot of details (that you don't care about right now and think they are stupid, until you actually do and then you are happy that someone thought of them for you) are being taken into consideration. Anyway, if you just want to get a logging to terminal working with slog, consider using a wrapper crate like sloggers instead.

Note: A lot of users gets bitten by the fact that slog::Logger::root(...) requires a drain that is safe to send and share across threads (Send+Sync). With shared resource like terminal or a file to which you log, a synchronization needs to be taken care of. If you get compilation errors around Sync or Send you are doing something wrong around it.

Using Decorator open trait, user can implement outputting using different colors, terminal types and so on.

Synchronization via PlainSyncDecorator

This logger works by synchronizing on the IO directly in PlainSyncDecorator. The formatting itself is thread-safe.

use slog::*;

let plain = slog_term::PlainSyncDecorator::new(std::io::stdout());
let logger = Logger::root(
    slog_term::FullFormat::new(plain)
    .build().fuse(), o!()
);

info!(logger, "Logging ready!");

Synchronization via slog_async

This drain puts logging into a separate thread via slog_async::Async: formatting and writing to terminal is happening in a one dedicated thread, so no further synchronization is required.

use slog::{Drain, o, info};

let decorator = slog_term::TermDecorator::new().build();
let drain = slog_term::CompactFormat::new(decorator).build().fuse();
let drain = slog_async::Async::new(drain).build().fuse();

let log = slog::Logger::root(drain, o!());

info!(log, "Logging ready!");

Synchronization via Mutex

This drain synchronizes by wrapping everything in a big mutex (yes, Mutex<Drain> implements a Drain trait). This is kind of slow, but in scripting languages like Ruby or Python pretty much the whole code is running in a one huge mutex and noone seems to mind, so I'm sure you're going to get away with this. Personally, I am a bit sad, that I've spent so much effort to give you tools to make your code as efficient as possible, and you choose this. ಠ_ಠ . But I'm here to serve, not to tell you what to do.

use slog::{Drain, o, info};

let decorator = slog_term::TermDecorator::new().build();
let drain = slog_term::CompactFormat::new(decorator).build();
let drain = std::sync::Mutex::new(drain).fuse();

let log = slog::Logger::root(drain, o!());

info!(log, "Logging ready!");

Dependencies

~1–10MB
~74K SLoC