#logging

log-reroute

Support to change logging target for the log crate

9 releases

0.1.8 Sep 12, 2021
0.1.7 Feb 21, 2021
0.1.6 Nov 17, 2020
0.1.5 Dec 25, 2019
0.1.1 Jul 23, 2018

#164 in Debugging

Download history 113/week @ 2021-08-11 205/week @ 2021-08-18 112/week @ 2021-08-25 164/week @ 2021-09-01 516/week @ 2021-09-08 452/week @ 2021-09-15 237/week @ 2021-09-22 168/week @ 2021-09-29 128/week @ 2021-10-06 305/week @ 2021-10-13 232/week @ 2021-10-20 118/week @ 2021-10-27 326/week @ 2021-11-03 114/week @ 2021-11-10 261/week @ 2021-11-17 138/week @ 2021-11-24

887 downloads per month
Used in 4 crates (3 directly)

Apache-2.0/MIT

9KB
68 lines

Log-reroute

Actions Status codecov docs

The log allows setting the target logger, but only once during the lifetime of the application. This library helps with that by providing a logger proxy. The logger behind the proxy can be switched as necessary.


lib.rs:

Crate to reroute logging messages at runtime.

The log logging facade allows to set only a single destination during the whole lifetime of program. If you want to change the logging destination multiple times, you can use Reroute (either directly, or through the init and reroute functions).

This may be useful if you want to log to stderr before you know where the main logs will go.

use fern::Dispatch;
use log::{info, LevelFilter};

fn main() {
    // Enable logging of Debug and more severe messages.
    log::set_max_level(LevelFilter::Debug);
    info!("This log message goes nowhere");
    log_reroute::init().unwrap();
    info!("Still goes nowhere");
    // Log to stderr
    let early_logger = Dispatch::new().chain(std::io::stderr()).into_log().1;
    log_reroute::reroute_boxed(early_logger);
    info!("This one goes to stderr");
    // Load file name from config and log to that file
    let file = tempfile::tempfile().unwrap();
    let logger = Dispatch::new().chain(file).into_log().1;
    log_reroute::reroute_boxed(logger);
    info!("And this one to the file");
    // Stop logging
    log_reroute::reroute(log_reroute::Dummy);
}

Dependencies

~310KB