Crate parallel_logger

source ·
Expand description

§Parallel Logger

This module provides a ParallelLogger struct that implements the log::Log trait.

The ParallelLogger forwards log messages to other loggers, but it does so in a separate thread. This can be useful in scenarios where logging can be a bottleneck, for example, when the logger writes to a slow output (like a file or a network), or when there are a lot of log messages or in a realtime scenario.

As an async framework might not yet be available when the logger is set up, async/await is not used.

§Usage

First, create the actual loggers that you want to use, like for example TermLogger, WriteLogger or even CombinedLogger from the simplelog crate. Any log::Log implementation will work.
Then, initialize the ParallelLogger with the maximum log level and the actual loggers:

use log::LevelFilter;
use simplelog::TermLogger;
use parallel_logger::ParallelLogger;

let term_logger = TermLogger::new(
    LevelFilter::Info,
    simplelog::Config::default(),
    simplelog::TerminalMode::Mixed,
    simplelog::ColorChoice::Auto
);
// create more loggers here if needed and add them to the vector below

ParallelLogger::init(LevelFilter::Info, vec![term_logger]);

Now, you can use the log crate’s macros (error!, warn!, info!, debug!, trace!) to log messages. The ParallelLogger will forward these messages to the actual loggers in a separate thread.

Structs§

  • A log::Log implementation that executes all logging on a separate thread.

    Simply pass the actual loggers in the call to ParallelLogger::init.