emabee / flexi_logger

A flexible logger for rust programs that can write to stderr, stdout, and/or to log files
Apache License 2.0
307 stars 50 forks source link

Option to disable stderr/stdout output? #142

Closed hasezoey closed 1 year ago

hasezoey commented 1 year ago

I currently have flexi_logger in a mixed cli / tui application and would like to disable output to stderr after the logger has already been configured and otherwise duplicate to both file and stderr.

Why?

because stderr / stdout logging while being in a tui-rs (EnterAlternateScreen) is not really the best experience

Example without any other things being rendered by tui-rs: Screenshot_20230727_201026

currently as a workaround i will use the default behavior of "when a log_to_file is given, dont log to stderr" (no duplicate)

What would i like?

something along the lines of

let logger_handle = /*...*/;
logger_handle.disable_stderr(); // along with a stdout equivalent
logger_handle.enable_stderr();
// or
logger_handle.disable(enum::stderr); // enum also has "stdout"
logger_handle.enable(enum::stderr);

or some kind of reconfigure akin to reset_flw

emabee commented 1 year ago

There are now methods LoggerHandle::adapt_duplication_to_stderr and LoggerHandle::adapt_duplication_to_stdout which take a Duplicate as parameter. Hope that fits :-)

hasezoey commented 1 year ago

sadly doing the following:

logger_handle
    .adapt_duplication_to_stderr(flexi_logger::Duplicate::None)
    .expect("Expected to be able to adapt duplication");

does not actually fully address my issue, i wanted a way to disable stderr / stdout (or maybe any other specific target) output of logging regardless of if duplication is activated (in my case logging to file, and so duplication is conditional - a cli argument)

currently if run without duplication, a Err appears (for testing via .expect):

thread 'main' panicked at 'Expected to be able to adapt duplication: NoFileLogger', crates/mh/src/main.rs:190:18

and in the case of the err being ignored, it still prints to the stderr, which messes up the TUI, my workaround for now is / was to not duplicate logging and if no file-logging is enabled to fully disable the logger via logger_handle.set_new_spec(LogSpecification::off());

emabee commented 1 year ago

Wouldn't it be sufficient for your case to not call logger_handle.adapt_duplication_to_stderr if duplication was not configured in the first place?

hasezoey commented 1 year ago

well i just think the following is not really a great implementation:

info!("Disabling stderr logging");

if !cli_matches.log_options.log_to_file {
    logger_handle.set_new_spec(LogSpecification::off());
} else {
    let _ = logger_handle.adapt_duplication_to_stderr(flexi_logger::Duplicate::None);
}

when what i suggested in the original description would be:

info!("Disabling stderr logging");

logger_handle.disable(enum::stderr); // or any of the other proposed versions

but i guess this works for now