Skip to main content

Crate nanologger

Crate nanologger 

Source
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:

LevelColorTag
ErrorRed bold[ERROR]
WarnYellow bold[WARN]
InfoGreen bold[INFO]
DebugBlue bold[DEBUG]
TraceMagenta 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:

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) prepends HH:MM:SS.mmm via 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 changesset_level adjusts the global level after init
  • Env varNANOLOGGER_LEVEL sets 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 Debug level.
error
Logs a message at the Error level.
info
Logs a message at the Info level.
trace
Logs a message at the Trace level.
warn
Logs a message at the Warn level.

Structs§

InitError
Error returned when attempting to initialize the logger more than once.
Logger
The global logger. Immutable after initialization. The global logger. Immutable after initialization.
LoggerBuilder
Builder for configuring and initializing the global Logger. Builder for configuring and initializing the global Logger.
ParseLevelError
Error returned when parsing an invalid log level string.
StyledString
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§

Colorize

Functions§

init
Convenience function: initialize the global logger with default settings (level = Info).
matches_module_filter
Returns true if a message from module_path should be emitted given the allow and deny lists.
set_level
Changes the global log level at runtime.
style
Wraps any Display type in a StyledString for chainable styling.