Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom logging level #166

Closed
konqueror1 opened this issue Jun 24, 2024 · 1 comment
Closed

Custom logging level #166

konqueror1 opened this issue Jun 24, 2024 · 1 comment
Labels

Comments

@konqueror1
Copy link

I am using flexy_logger for simultaneously log to file and stdout. I try using that macro:

macro_rules! bold_red {
($($arg:tt)) => (
log::log!(log::Level::Info, "{}",
format_args!("\x1b[1;31m{}\x1b[0m", format_args!($($arg)
))
);
)
}

Stdout works perfect but when writing to file ANSI escape codes are in the file. I can strip codes with regex as workaround but I am sure there is more idiomatic way to do it.

@emabee
Copy link
Owner

emabee commented Jun 30, 2024

Yes, you can achieve what you need with something like this:

use flexi_logger::{Duplicate, FileSpec, Logger};
use log::{debug, error, info, trace, warn};

fn main() {
    let _logger = Logger::try_with_env_or_str("info")
        .unwrap()
        .log_to_file(FileSpec::default())
        .duplicate_to_stdout(Duplicate::All)
        .start()
        .unwrap();

    error!("Test error");
    warn!("Test warning");
    info!("Test info");
    debug!("Test debug");
    trace!("Test trace");
}

The program prints to stdout in colors and writes to the file without color escape codes.

@emabee emabee closed this as completed Jun 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants