Drakulix / simplelog.rs

Simple Logging Facility for Rust
https://docs.rs/simplelog/
Apache License 2.0
414 stars 71 forks source link

[feat] Add set_enable_paris_formatting() ConfigBuilder method to control whether… #126

Closed shimaowo closed 1 year ago

shimaowo commented 1 year ago

… a given logger uses or strips paris formatting. Only available with the paris feature, defaults to enabled for backwards compatibility. Fixes #98 and #112

This basically just defers the paris formatting until actual log time, rather than having it applied as part of the initial macro. This allows specifying different values per-logger (via their Config), which can then be passed to the new paris method.

paris_macros was removed as it is no longer used.

I chose to add a new flag, rather than use write_log_enable_colors, as the setter for that is already gated by the ansi_term feature, and presumably already has a use.

The extra 'return' is due to rust not understanding that a terminating statement is an implicit return if there is a later #[cfg] block.

I did not include a test as there are no paris tests currently.

Simple example:

use log::*;
use simplelog::*;
use std::fs::File;

fn main() {
    CombinedLogger::init(vec![
        TermLogger::new(
            LevelFilter::Info,
            Config::default(),
            TerminalMode::Mixed,
            ColorChoice::Auto),

        WriteLogger::new(
            LevelFilter::Debug,
            ConfigBuilder::new()
                .set_enable_paris_formatting(false)
                .build(),
            File::create("log.txt").unwrap()
        )
    ]).unwrap();

    info!("Here is some <blue>colored</> text.");
}

The TermLogger output will be colored as usual; the WriteLogger output will simply be the message "Here is some colored text." with no markup or formatting.

shimaowo commented 1 year ago

@Drakulix So the tests fail this way even in master, due to the discrepancy between how the log import is handled. I could try to address that here, but it's kind of orthogonal.

shimaowo commented 1 year ago

Actually this is easier to fix with this changeset than it is in master, because paris no longer requires special handling.

This PR will go through one of these days!