What is slog?
slog (2016) is a structured logging framework for
Rust. Log records carry key-value fields (not just formatted strings),
and output flows through composable drains (terminal, JSON file,
async writer, etc.).
It predates tracing and was popular for services that wanted
JSON logs and contextual Logger handles passed through call
chains. New Tokio-centric projects often choose
tracing instead, but
slog remains mature and stable.
Simple example
use slog::{o, Drain, Logger};
use slog_term::{CompactFormat, TermDecorator};
fn main() {
let decorator = TermDecorator::new().build();
let drain = CompactFormat::new(decorator).build().fuse();
let log = Logger::root(drain, o!());
slog::info!(log, "request handled"; "user_id" => 42, "latency_ms" => 12);
}
Fields user_id and latency_ms are structured —
JSON drains serialize them as separate columns/keys for log aggregators.
Drains and loggers
- Drain — where records go (stdout, file, syslog). Drains can be chained (filter → format → write).
- Logger — handle with default context; child loggers add fields (similar in spirit to tracing spans, but scope is manual).
- slog-async — non-blocking drain for high-throughput apps.
Comparison table: tracing vs log vs slog vs log4rs.