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, the parallel execution mode, and the actual loggers:

use log::LevelFilter;
use simplelog::TermLogger;
use parallel_logger::{ ParallelLogger, ParallelMode };

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, ParallelMode::Sequential, vec![term_logger]);

ParallelMode::Sequential will execute all actual loggers in sequence on a separate thread, while ParallelMode::Parallel will execute each actual logger in its own thread. Unless there is really high contention on the loggers, ParallelMode::Sequental will be sufficient for most use cases.

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 as configured .

Structs§

  • A log::Log implementation that executes all logging in parallel.
    Simply pass the actual loggers in the call to ParallelLogger::init.
  • A handle that can be used to shut down the ParallelLogger

Enums§

  • The mode in which the logger will process the actual loggers