Expand description
§nanologger
A minimal logger for Rust.
nanologger provides five leveled log macros (error!, warn!, info!,
debug!, trace!) that write colored, formatted output to stderr. It
supports optional timestamps, source locations, thread info, module
filtering, file logging, and combined multi-output configurations.
§Quick start
use nanologger::{LoggerBuilder, LogLevel};
LoggerBuilder::new()
.level(LogLevel::Trace)
.timestamps(true)
.init()
.unwrap();
nanologger::error!("something went wrong: {}", "disk full");
nanologger::warn!("retries remaining: {}", 3);
nanologger::info!("server started on port {}", 8080);
nanologger::debug!("request payload: {:?}", vec![1, 2, 3]);
nanologger::trace!("entering function");Or with defaults (Info level, stderr, no timestamps):
nanologger::init().unwrap();
nanologger::info!("hello");§Log levels
LogLevel variants ordered by decreasing severity:
| Level | Color | Tag |
|---|---|---|
Error | Red bold | [ERROR] |
Warn | Yellow bold | [WARN] |
Info | Green bold | [INFO] |
Debug | Blue bold | [DEBUG] |
Trace | Magenta bold | [TRACE] |
Messages below the configured level are silently discarded. Colors are automatically disabled when stderr is not a TTY.
§Output destinations
LogOutput controls where log messages go:
LogOutput::term— stderr with color supportLogOutput::writer— anyimpl Write + Send(files, buffers, etc.), plain textLogOutput::test— viaprint!(), captured by Rust’s test harness
Multiple outputs can be added to a single logger, each with its own level filter:
use nanologger::{LogLevel, LogOutput, LoggerBuilder};
use std::fs::File;
let file = File::create("app.log").unwrap();
LoggerBuilder::new()
.level(LogLevel::Trace)
.add_output(LogOutput::term(LogLevel::Warn)) // terminal: Warn+
.add_output(LogOutput::writer(LogLevel::Trace, file)) // file: everything
.init()
.unwrap();§Optional features
- Timestamps —
.timestamps(true)prependsHH:MM:SS.mmmvia nanotime - Source location —
.source_location(true)appends[file:line]after the level tag - Thread info —
.thread_info(true)shows(thread-name)or(ThreadId(N)) - Module filtering —
.module_allow()/.module_deny()for prefix-based filtering - Runtime level changes —
set_leveladjusts the global level after init - Env var —
NANOLOGGER_LEVELsets the default level (case-insensitive)
§log facade integration
Enable the log feature to use nanologger as a backend for the log crate:
[dependencies]
nanologger = { version = "0.1.0", features = ["log"] }When initialized, nanologger registers itself via log::set_logger, so
libraries using log::info!() etc. route through nanologger automatically.
§Colored message content
nanologger re-exports Colorize, style, and StyledString from
nanocolor, so you can style log
message content without an extra dependency:
use nanologger::{info, Colorize, style};
info!("listening on {}", "127.0.0.1:3000".cyan());
let v = style(format!("v{}.{}.{}", 0, 1, 0)).cyan().bold();
info!("running nanologger {}", v);Macros§
- debug
- Logs a message at the
Debuglevel. - error
- Logs a message at the
Errorlevel. - info
- Logs a message at the
Infolevel. - trace
- Logs a message at the
Tracelevel. - warn
- Logs a message at the
Warnlevel.
Structs§
- Init
Error - Error returned when attempting to initialize the logger more than once.
- Logger
- The global logger. Immutable after initialization. The global logger. Immutable after initialization.
- Logger
Builder - Builder for configuring and initializing the global Logger. Builder for configuring and initializing the global Logger.
- Parse
Level Error - Error returned when parsing an invalid log level string.
- Styled
String - A string with accumulated ANSI color and style information.
Enums§
- LogLevel
- Log severity levels, ordered from highest to lowest severity.
- LogOutput
- Represents a log output destination.
Traits§
Functions§
- init
- Convenience function: initialize the global logger with default settings (level = Info).
- matches_
module_ filter - Returns
trueif a message frommodule_pathshould be emitted given the allow and deny lists. - set_
level - Changes the global log level at runtime.
- style
- Wraps any
Displaytype in aStyledStringfor chainable styling.