Expand description
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!");Structs§
- Compact
Format - Compact terminal-output formatting
Drain - Compact
Format Builder - Streamer builder
- Compact
Format Serializer - The Compact format serializer
- Counting
Writer - Wrapper for
Writetypes that counts total bytes written. - Full
Format - Terminal-output formatting
Drain - Full
Format Builder - Streamer builder
- Plain
Decorator - Plain (no-op)
Decoratorimplementation - Plain
Record Decorator - Record decorator used by
PlainDecorator - Plain
Sync Decorator - PlainSync
Decoratorimplementation - Plain
Sync Record Decorator RecordDecoratorused byPlainSyncDecorator- Serializer
- Serializer for the lines
- Term
Decorator Decoratorimplemented usingtermcrate- Term
Decorator Builder TermDecoratorbuilder- Term
Record Decorator - Record decorator used by
TermDecorator - Test
Stdout Writer - Replacement for
std::io::stdout()for when output capturing by rust’s test harness is required.
Traits§
- Decorator
- Output decorator
- Record
Decorator - Per-record decorator
- Thread
Safe Header Fn - Threadsafe header formatting function type
- Thread
Safe Timestamp Fn - Threadsafe timestamp formatting function type
Functions§
- print_
msg_ header - Returns
trueif message was not empty - term_
compact - Create a
CompactFormatdrain with default settings - term_
full - Create a
FullFormatdrain with default settings - timestamp_
local - Default local timezone timestamp function
- timestamp_
utc - Default UTC timestamp function