pylogging
A small, ergonomic logging library for Rust, inspired by Python's
logging module.
It is published as the pylogging crate but imported as logging, so
the API reads the way it does in Python.
Features
- Named loggers in a process-global registry (
Logger::get("my::module")). - Inheritance from a configurable root logger (handlers + level).
- Level filtering (
Debug,Info,Warning,Error,Critical) with cheap, allocation-free rejection of messages below the threshold. - Pattern-based formatting with field placeholders, width/alignment, and
truncation, e.g.
"%(timestamp) [%(level)-8] %(name)-12 | %(message)". - Pluggable handlers via the
Handlertrait; aStreamHandlerwrites to anystd::io::Writesink (stdout, files, in-memory buffers, ...). - Transformers for post-processing a rendered line (e.g. ANSI colors).
Installation
[]
= "0.1"
The crate is imported as logging:
use ;
Quick start
use ;
let logger = get;
logger
.add_handler
.unwrap;
logger.info;
// prints: INFO: hello
Configure the root logger once and every logger inherits it:
use ;
let mut formatter = new;
formatter.set_time_format;
let root = root;
root.add_handler.unwrap;
root.set_level;
let logger = get;
logger.info;
See examples/quickstart.rs for a fuller demo
(including per-level ANSI colors). Run it with:
Pattern syntax
A pattern is literal text interspersed with %(field) placeholders. A
placeholder may be followed by a spec controlling width and alignment:
| Spec | Meaning |
|---|---|
%(name) |
The value of name, or "" if absent. |
%(name)8 |
Right-align to a minimum width of 8. |
%(name)-8 |
Left-align to a minimum width of 8. |
%(name).4 |
Truncate to at most 4 characters. |
%(name)-8.4 |
Truncate to 4, then left-pad to width 8. |
Common fields populated per record: message, level, name, timestamp,
thread.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.