slog-term

Terminal drain and formatter for slog-rs, with full and compact colored output

Library
Cargo
v2.9.2
19stars
MPL-2.0 OR MIT OR Apache-2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
30/100Needs Attention
Development Activity0
Maintenance20
Community28
Maturity60
Momentum12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
61/100Good
Architecture68
Code Quality62
Innovation55
Learning Curve58

slog-term is the terminal output backend for the slog structured-logging ecosystem in Rust. It turns slog’s structured log records into human-readable, optionally colorized text streamed to stdout or stderr, so developers get readable console logs during local development while still building their logging pipeline on slog’s structured Drain model.

It ships two output formats out of the box — a full, multi-line format for verbose debugging and a compact format that groups repeated key-values to cut down noise in busy logs — plus a set of Decorator implementations (TermDecorator, PlainDecorator, PlainSyncDecorator) that control how colors and terminal capabilities are detected and applied.

What You Get

  • FullFormat — a verbose, multi-line drain that prints timestamp, level, message, and all key-value pairs, with optional file:line:column location tagging
  • CompactFormat — a denser drain that remembers previously-printed key-values and only prints deltas, reducing repetition in high-volume logs
  • TermDecorator — auto-detects TTY and color support via the term crate, with force_color()/force_plain() and stdout()/stderr() builder overrides
  • PlainDecorator and PlainSyncDecorator — uncolored decorators for writing to files or piping to other tools, with the sync variant safe to share across threads without an external mutex
  • Convenience constructors term_compact() and term_full() for zero-configuration default setups
  • Pluggable timestamp functions (timestamp_local, timestamp_utc, or a custom closure) and a custom header-printer hook for changing the line-prefix format

Common Use Cases

  • Human-readable console logging during local development, layered on top of slog’s structured logging API
  • Colorized CLI tool output where log level (INFO/WARN/ERROR) needs to be visually distinguishable at a glance
  • Compact, low-noise logging for long-running services where the same key-values repeat across many log lines
  • Writing plain, uncolored log text to a file or downstream pipe via PlainDecorator while keeping colored output for the terminal

Under The Hood

Architecture - The crate is built around slog’s Drain trait, with FullFormat<D> and CompactFormat<D> (both generic over a Decorator implementation D) as the two entry-point drains, defined in the single src/lib.rs file. Each drain calls into a shared Decorator::with_record callback that hands back a RecordDecorator (an io::Write extended with start_level/start_key/reset styling hooks), which the drain’s internal Serializer (FullFormat) or CompactFormatSerializer (CompactFormat, which keeps a RefCell<Vec<(Vec<u8>, Vec<u8>)>> history to diff key-values across calls) writes formatted text into. TermDecorator, PlainDecorator, and PlainSyncDecorator are the three concrete Decorators, each wrapping a different I/O strategy (raw term::Terminal, a bare RefCell<W>, or a Arc<Mutex<W>> for thread-safe sharing) behind the same trait, so drains are decoupled from how bytes actually reach the terminal.

Tech Stack - Pure Rust (96.8% of the codebase, edition 2018, MSRV 1.63), with slog as the core structured-logging dependency it formats for, term for cross-platform terminal color/attribute control, is-terminal for TTY detection, time and an optional chrono dependency for timezone-aware timestamps, and thread_local for per-thread state. An optional nested-values Cargo feature pulls in erased-serde/serde/serde_json to support slog’s nested-value serialization. No async runtime or web framework dependencies — the crate is deliberately narrow in scope.

Code Quality - The crate has one integration test file (tests/term.rs) plus six runnable examples under examples/ covering compact/full, colored/plain, sync/async, and file-output configurations, which double as living documentation more than as a test suite. #![warn(missing_docs)] is enabled at the crate root, and public types carry doc comments with runnable doctests embedded directly in src/lib.rs’s module-level docs. There is no dedicated unit-test coverage of the formatting/serialization logic itself beyond what the integration test and doctests exercise.

API Design - The builder pattern (FullFormat::new(decorator).build(), TermDecoratorBuilder::new().stderr().force_color().build()) keeps construction readable and chainable, and term_compact()/term_full() give a one-line zero-config path for the common case. The Decorator/RecordDecorator trait split cleanly separates ‘how to get a writer for this record’ from ‘how to write styled text’, which is what lets PlainDecorator, PlainSyncDecorator, and TermDecorator share the same drains. The tradeoff is that CompactFormat is explicitly !Sync (its history RefCell requires external synchronization via Mutex or slog_async::Async), which the docs call out clearly but which is easy for newcomers to slog to trip over.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search