quickstart/quickstart.rs
1//! The simplest possible use of the `logging` crate (published as `pylogging`).
2//!
3//! Run it with:
4//!
5//! ```sh
6//! cargo run --example basic
7//! ```
8//!
9//! For a fuller demo (custom timestamp format, per-level ANSI colors, and
10//! level filtering), see `examples/quickstart.rs`.
11
12use logging::{Logger, StreamHandler};
13
14fn main() {
15 // Get a named logger from the global registry. Calling `get` with the same
16 // name again anywhere in the program returns the same logger.
17 let logger = Logger::get("basic");
18
19 // Attach a handler that writes to stdout using a simple pattern.
20 // `%(level)` and `%(message)` are field placeholders.
21 logger
22 .add_handler(StreamHandler::with_pattern(
23 std::io::stdout(),
24 "%(level): %(message)",
25 ))
26 .expect("handler lock should not be poisoned at startup");
27
28 // Emit a few messages. The default level is `Info`, so `debug` is dropped.
29 logger.info("hello from a named logger");
30 logger.warning("something to keep an eye on");
31 logger.error("something went wrong");
32}