Skip to main content

Crate logging

Crate logging 

Source
Expand description

A small logging library inspired by Python’s logging module.

The pieces fit together as a pipeline:

  • Logger is the user-facing entry point (info, warning, …). It owns a level and a set of handlers, and builds a Record per call.
  • Handler is a sink (e.g. StreamHandler) that renders a record with its Formatter and writes it somewhere.
  • Formatter turns a Record into a string from a pattern like "%(timestamp) - %(level) - %(message)".
  • Level is the severity enum; records below a logger’s level are dropped.

Loggers are stored in a process-global registry, so Logger::get returns a shared logger by name, and Logger::root is the parent of all loggers.

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

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

Structs§

Formatter
Renders a Record into a string according to a pattern.
Logger
A named logger with a severity threshold and a set of Handlers.
ParseLevelError
The error returned when a string cannot be parsed into a Level.
StreamHandler
A Handler that writes formatted records to any Write sink, guarded by a Mutex so a single stream can be shared across threads.

Enums§

Level
Severity level of a log message, ordered from least to most severe.

Traits§

Handler
A sink for log records. Implementors render the record (via their Formatter) and deliver it somewhere (stdout, a file, …).

Type Aliases§

Record
A single log record: a map of field name to value, both borrowed for the duration of the logging call (e.g. "message", "level", "timestamp").