Skip to main content

Crate minimal_logger

Crate minimal_logger 

Source
Expand description

Minimal-resource logger for Rust applications.

minimal_logger provides a small, thread-buffered logger with optional file output, auto flush, and platform-aware log rotation.

§Example

use log::{error, info};

fn main() {
    minimal_logger::init().expect("failed to initialise logger");
    info!("application started");
    error!("shutdown due to error");
    minimal_logger::shutdown();
}

§Rotation detection

Each thread-local BufWriter<FileWriter> holds an Arc<LogFile> inside FileWriter. On every log call, load_full() returns the current Arc<LogFile>. If Arc::ptr_eq fails, a rotation has occurred:

  1. Flush old BufWriter → drains remaining bytes to old file.
  2. Replace with new BufWriter::with_capacity(capacity, FileWriter(new_arc)). No separate reopen flag check is needed inside write_record.

§Flush model

TriggerMechanism
Buffer fullBufWriter flushes automatically when internal
buffer capacity is reached
PeriodicBackground thread sets FLUSH_FLAG; next log call
calls bw.flush()
Thread exitBufWriter::drop() calls flush() automatically
Explicitshutdown() / Log::flush() calls bw.flush()

§Environment variables

VariableDefaultDescription
RUST_LOGinfoLevel + per-target filters
RUST_LOG_FILEstderrAbsolute path to log file
RUST_LOG_BUFFER_SIZE4096Per-thread BufWriter capacity
RUST_LOG_FLUSH_MS1000Periodic flush interval (ms)
RUST_LOG_FORMAT"{timestamp} [{level:<5}] T[{thread_name}] [{file}:{line}] {args}"Log message format template (timestamp is fixed 6-digit microseconds)

Supported format fields: timestamp, level, thread_name, target, module_path, file, line, args.

Structs§

MinimalLogger

Functions§

init
Initialise the logger and start the background flush thread. Call once at program startup before any log macros are used.
shutdown
Flush the calling thread’s BufWriter to the kernel.