pylogging 0.1.1

A small, ergonomic logging library inspired by Python's logging module.
Documentation
  • Coverage
  • 100%
    19 out of 19 items documented1 out of 1 items with examples
  • Size
  • Source code size: 70.91 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 914.61 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 8s Average build duration of successful builds.
  • all releases: 7s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • dt-ss/pylogging
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • dt-ss

pylogging

Crates.io Documentation License

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 Handler trait; a StreamHandler writes to any std::io::Write sink (stdout, files, in-memory buffers, ...).
  • Transformers for post-processing a rendered line (e.g. ANSI colors).

Installation

[dependencies]
pylogging = "0.1"

The crate is imported as logging:

use logging::{Formatter, Level, Logger, StreamHandler};

Quick start

use logging::{Logger, StreamHandler};

let logger = Logger::get("example");
logger
    .add_handler(StreamHandler::with_pattern(std::io::stdout(), "%(level): %(message)"))
    .unwrap();
logger.info("hello");
// prints: INFO: hello

Configure the root logger once and every logger inherits it:

use logging::{Formatter, Level, Logger, StreamHandler};

let mut formatter = Formatter::new("%(timestamp) [%(level)-8] %(name)-12 | %(message)");
formatter.set_time_format("%Y-%m-%d %H:%M:%S");

let root = Logger::root();
root.add_handler(StreamHandler::new(formatter, std::io::stdout())).unwrap();
root.set_level(Level::Debug);

let logger = Logger::get("my::module");
logger.info("inherits root's handler and level");

See examples/quickstart.rs for a fuller demo (including per-level ANSI colors). Run it with:

cargo run --example quickstart

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

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.