13 releases (6 breaking)
| 0.7.1 | Oct 11, 2025 |
|---|---|
| 0.6.0 | Oct 8, 2025 |
| 0.4.1 | Jun 21, 2025 |
| 0.2.0 | Jan 13, 2025 |
| 0.1.3 | Oct 16, 2024 |
#277 in Debugging
75KB
1.5K
SLoC
winston
A fast, flexible logging library for Rust inspired by Winston.js.
Overview
Winston provides structured logging with composable transports, formats, and levels. Built on async foundations with intelligent backpressure handling, it's designed for both development convenience and production performance.
Quick Start
Simple Console Logging
use winston::{log, Logger, transports::stdout};
fn main() {
let logger = Logger::builder()
.level("info")
.transport(stdout())
.build();
winston::init(logger);
log!(info, "Application started");
log!(warn, "Low disk space", usage = 92);
winston::close();
}
Multi-Transport Logger
use winston::{Logger, log, format::{timestamp, json, chain}, transports::{stdout, File}};
fn main() {
let logger = Logger::builder()
.level("debug")
.format(chain!(timestamp(), json()))
.transport(stdout())
.transport(File::builder().filename("app.log").build())
.build();
log!(logger, info, "Logging to console and file");
}
Core Concepts
LogInfo - Structured Log Data
Every log message is represented by a LogInfo struct containing level, message, and metadata:
// Simple log
let info = LogInfo::new("info", "User authenticated");
// With metadata
let info = info.with_meta("user_id", 12345)
.with_meta("session_id", "abc123");
Transports - Where Logs Go
Transports define output destinations. Each implements the Transport trait:
pub trait Transport: Send + Sync {
fn log(&self, info: LogInfo);
fn flush(&self) -> Result<(), String> { Ok(()) }
fn query(&self, options: &LogQuery) -> Result<Vec<LogInfo>, String> { Ok(Vec::new()) }
}
Built-in transports:
stdout()/stderr()- Console outputFile- File logging with querying supportWriterTransport- Generic writer for custom destinations
Multiple transports example:
// Using builder - all transports use logger's global level and format
let logger = Logger::builder()
.transport(stdout())
.transport(File::builder().filename("app.log").build())
.build();
// For custom level/format per transport, you have two options:
// Option 1: Runtime fluent API with logger.transport()
let logger = Logger::new(None);
let console_handle = logger.transport(stdout())
.with_level("info")
.with_format(colorize())
.add();
let file_handle = logger.transport(File::builder().filename("app.log").build())
.with_level("debug")
.with_format(json())
.add();
// Option 2: Pre-configure with LoggerTransport (works both build-time and runtime)
use winston::LoggerTransport;
let console_transport = LoggerTransport::new(stdout())
.with_level("info")
.with_format(colorize());
let file_transport = LoggerTransport::new(File::builder().filename("app.log").build())
.with_level("debug")
.with_format(json());
// Use in builder (build-time)
let logger = Logger::builder()
.transport(console_transport.clone())
.transport(file_transport.clone())
.build();
// Or add at runtime
let logger = Logger::new(None);
let console_handle = logger.add_transport(console_transport);
let file_handle = logger.add_transport(file_transport);
Levels - Message Priority
Winston uses RFC 5424 severity levels (lower = more critical):
levels: {
error: 0, // System errors
warn: 1, // Warnings
info: 2, // General info
debug: 3, // Debug details
trace: 4 // Verbose tracing
}
Set minimum level to control verbosity:
let logger = Logger::builder()
.level("info") // Logs info, warn, error (filters out debug, trace)
.build();
Formats - Message Styling
Winston uses the powerful logform library for message formatting through composable format chaining:
use winston::format::{timestamp, json, colorize, chain};
// Using the chain method
let logger = Logger::builder()
.format(
timestamp()
.with_format("%Y-%m-%d %H:%M:%S")
.chain(colorize())
.chain(json())
)
.build();
// Using the chain! macro for cleaner syntax
let logger = Logger::builder()
.format(chain!(
timestamp().with_format("%Y-%m-%d %H:%M:%S"),
colorize(),
json()
))
.build();
Per-transport formatting:
let logger = Logger::builder()
.transport(stdout()) // Uses logger's global format
.build();
// Or configure per-transport
let logger = Logger::new(None);
logger.transport(stdout())
.with_format(chain!(
timestamp().with_format("%H:%M:%S"),
colorize()
))
.add();
logger.transport(File::builder().filename("app.log").build())
.with_format(chain!(
timestamp().with_format("%Y-%m-%d %H:%M:%S"),
json()
))
.add();
Advanced Features
Custom Log Levels
Define domain-specific severity levels:
use std::collections::HashMap;
let custom_levels = HashMap::from([
("critical", 0),
("high", 1),
("medium", 2),
("low", 3)
]);
let logger = Logger::builder()
.levels(custom_levels)
.build();
Create custom logging methods and macros:
winston::create_log_methods!(critical, high, medium, low);
winston::create_level_macros!(critical, high, medium, low);
// Now you can use:
logger.critical("System failure", None);
high!(logger, "Priority task failed", retries = 3);
Dynamic Transport Management
Add and remove transports at runtime:
let logger = Logger::new(None);
// Add transports and get handles
let console_handle = logger.add_transport(stdout());
let file_handle = logger.transport(File::builder().filename("app.log").build())
.with_level("debug")
.add();
// Later, remove specific transports
logger.remove_transport(console_handle); // Stop console logging
logger.remove_transport(file_handle); // Stop file logging
Backpressure Management
Control behavior when the log buffer fills up:
use winston::BackpressureStrategy;
let logger = Logger::builder()
.channel_capacity(1000)
.backpressure_strategy(BackpressureStrategy::DropOldest) // or Block, DropCurrent
.build();
Strategy recommendations:
Block- Best for critical logs where no messages should be lostDropOldest- Good for high-volume applications where recent logs matter mostDropCurrent- Suitable when preserving historical context is more important
Log Querying
Retrieve historical logs from queryable transports:
use winston::LogQuery;
let query = LogQuery::new()
.from("2 hours ago")
.until("now")
.levels(vec!["error", "warn"])
.search_term("database")
.limit(50);
let results = logger.query(query)?;
Query options:
from/until- Time range (supports natural language viaparse_datetime)levels- Filter by severitysearch_term- Text search in messageslimit/start- Paginationorder-ascordescfields- Projection (which fields to return)
Runtime Reconfiguration
Change logger settings dynamically:
logger.configure(
LoggerOptions::new()
.level("debug")
.transport(File::builder().filename("debug.log").build())
);
Custom Transports
Implement the Transport trait for custom destinations:
use winston::{Transport, LogInfo};
struct DatabaseTransport {
connection: DatabaseConnection,
}
impl Transport for DatabaseTransport {
fn log(&self, info: LogInfo) {
// Insert log into database
self.connection.execute("INSERT INTO logs ...", &info);
}
fn query(&self, options: &LogQuery) -> Result<Vec<LogInfo>, String> {
// Query logs from database
self.connection.query_logs(options)
}
}
Global vs Instance Logging
Global Logger (Singleton)
Convenient for application-wide logging:
use winston::{Logger, log, transports::stdout};
fn main() {
let logger = Logger::builder()
.transport(stdout())
.build();
winston::init(logger);
log!(info, "Using global logger");
winston::flush().unwrap(); // Important: flush before app exit
winston::close();
}
Logger Instances
Better for libraries or multi-tenant applications:
let logger = Logger::builder()
.transport(stdout())
.build();
log!(logger, info, "Using specific logger instance");
// Automatic cleanup on drop
Performance Tips
- Buffer sizing: Tune
channel_capacitybased on log volume - Transport selection: File transport is faster than stdout for high-volume logging
- Format efficiency: Simple formats are faster than complex chained formats
- Level filtering: Set appropriate minimum levels to avoid unnecessary processing
- Format chaining order: Place expensive formats (like colorization) last in the chain
Integration with the log Crate
Winston can also act as a backend for the widely used log facade.
This means that existing libraries and crates which emit logs via log will automatically route their output through Winston's transports and formatting system.
Enable the feature in Cargo.toml:
[dependencies]
winston = { version = "0.5", features = ["log-backend"] }
Then initialize Winston as the global logger:
use winston::{Logger, transports::stdout};
fn main() {
// Initialize winston
let logger = Logger::builder()
.transport(stdout())
.build();
winston::init(logger);
winston::register_with_log().unwrap();
log::info!("Hello from the log crate!");
log::warn!("This also goes through Winston transports");
winston::close();
}
Notes:
- Key–value metadata support from log is available with the
log-backend-kvfeature. - Winston's transports, levels, formats, and backpressure strategies apply seamlessly.
- Useful when integrating Winston into projects that already rely on the log ecosystem.
Installation
Add to your Cargo.toml:
[dependencies]
winston = "0.5"
Or use cargo:
cargo add winston
Contributing
Contributions welcome! Please submit issues and pull requests on GitHub.
License
MIT License
Acknowledgments
Inspired by the excellent Winston.js logging library.
Dependencies
~8–20MB
~246K SLoC