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 ;
Multi-Transport Logger
use ;
Core Concepts
LogInfo - Structured Log Data
Every log message is represented by a LogInfo struct containing level, message, and metadata:
// Simple log
let info = new;
// With metadata
let info = info.with_meta
.with_meta;
Transports - Where Logs Go
Transports define output destinations. Each implements the Transport trait:
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 = builder
.transport
.transport
.build;
// For custom level/format per transport, you have two options:
// Option 1: Runtime fluent API with logger.transport()
let logger = new;
let console_handle = logger.transport
.with_level
.with_format
.add;
let file_handle = logger.transport
.with_level
.with_format
.add;
// Option 2: Pre-configure with LoggerTransport (works both build-time and runtime)
use LoggerTransport;
let console_transport = new
.with_level
.with_format;
let file_transport = new
.with_level
.with_format;
// Use in builder (build-time)
let logger = builder
.transport
.transport
.build;
// Or add at runtime
let logger = new;
let console_handle = logger.add_transport;
let file_handle = logger.add_transport;
Levels - Message Priority
Winston uses RFC 5424 severity levels (lower = more critical):
levels:
Set minimum level to control verbosity:
let logger = builder
.level // 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 ;
// Using the chain method
let logger = builder
.format
.build;
// Using the chain! macro for cleaner syntax
let logger = builder
.format
.build;
Per-transport formatting:
let logger = builder
.transport // Uses logger's global format
.build;
// Or configure per-transport
let logger = new;
logger.transport
.with_format
.add;
logger.transport
.with_format
.add;
Advanced Features
Custom Log Levels
Define domain-specific severity levels:
use HashMap;
let custom_levels = from;
let logger = builder
.levels
.build;
Create custom logging methods and macros:
create_log_methods!;
create_level_macros!;
// Now you can use:
logger.critical;
high!;
Dynamic Transport Management
Add and remove transports at runtime:
let logger = new;
// Add transports and get handles
let console_handle = logger.add_transport;
let file_handle = logger.transport
.with_level
.add;
// Later, remove specific transports
logger.remove_transport; // Stop console logging
logger.remove_transport; // Stop file logging
Backpressure Management
Control behavior when the log buffer fills up:
use BackpressureStrategy;
let logger = builder
.channel_capacity
.backpressure_strategy // 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 LogQuery;
let query = new
.from
.until
.levels
.search_term
.limit;
let results = logger.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;
Custom Transports
Implement the Transport trait for custom destinations:
use ;
Global vs Instance Logging
Global Logger (Singleton)
Convenient for application-wide logging:
use ;
Logger Instances
Better for libraries or multi-tenant applications:
let logger = builder
.transport
.build;
log!;
// 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:
[]
= { = "0.5", = ["log-backend"] }
Then initialize Winston as the global logger:
use ;
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:
[]
= "0.5"
Or use cargo:
Contributing
Contributions welcome! Please submit issues and pull requests on GitHub.
License
MIT License
Acknowledgments
Inspired by the excellent Winston.js logging library.