log-lazy for Rust
A lazy logging crate with the same bitwise level model as the JavaScript
log-lazy package.
Core Idea
The entire closure passed to a log method is either executed or skipped. This lets production code keep detailed diagnostics without paying for formatting, serialization, or data collection when the level is disabled.
use ;
let log = with_level;
log.error;
debug_lazy!;
expensive_payload() is not called while debug is disabled.
Levels
The crate uses the same bit flags as the JavaScript package:
| Name | Value |
|---|---|
none |
0 |
fatal |
1 |
error |
2 |
warn |
4 |
info |
8 |
debug |
16 |
verbose |
32 |
trace |
64 |
silly |
128 |
all |
255 |
production |
7 |
development |
31 |
Combine levels with bitwise OR:
use ;
let mut log = with_level;
log.enable_level;
log.disable_level;
assert_eq!;
Custom Sinks
By default, fatal/error/warn messages go to stderr and the other levels go to
stdout. Use with_sink to route messages into another logger:
use ;
let log = with_sink;
log.info;
Preprocessors and Postprocessors
Use LogLazyOptions when you need a processor pipeline. Preprocessors receive
lazy LogArg values before they are evaluated. Postprocessors receive the
compiled message string immediately before the sink is called.
use ;
let log = with_options;
log.emit_args;
When no processors are configured, the existing closure-based logging path is used directly.