Skip to content

dt-ss/pylogging

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

A small, ergonomic logging library for Rust, inspired by Python's logging module.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages