Drakulix / simplelog.rs

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

Some `ConfigBuilder` options don't work as documented #66

Closed a-cristi closed 3 years ago

a-cristi commented 3 years ago

Either that, or I'm confused by the documentation.

For example, the documentation for set_max_level states that:

Set at which level and below the level itself shall be logged (default is Error)

Looking at the LevelFilter I have the impression that Off is the lowest level, while Trace is the highest, so set_max_level(Trace) should enable this for all levels. However, it seems to work in reverse.

use log::{debug, error, info, trace, warn};
use simplelog;
use simplelog::*;

fn main() {
TermLogger::init(LevelFilter::Trace,
    ConfigBuilder::new()
        .set_thread_mode(ThreadLogMode::Both)
        .set_max_level(LevelFilter::Trace)
        // Set everything else to `Off` so the output is simpler.
        .set_time_level(LevelFilter::Off)
        .set_thread_level(LevelFilter::Off)
        .set_target_level(LevelFilter::Off)
        .set_location_level(LevelFilter::Off)
        .build(),
    TerminalMode::Mixed).unwrap();
    trace!("This is a trace");
    debug!("This is a debug");
    info!("This is a info");
    warn!("This is a warn");
    error!("This is a error");
}

This outputs:

[TRACE] This is a trace
This is a debug
This is a info
This is a warn
This is a error

Setting the LevelFilter to Error display the level every time:

[TRACE] This is a trace
[DEBUG] This is a debug
[INFO] This is a info
[WARN] This is a warn
[ERROR] This is a error

The same is true for all other options. I'm not sure if this is a bug, because there seems to be a consistency between all the set_ methods, but things are not working as documented. This is especially confusing because the LevelFilter for init works as documented: if I change TermLogger::init(LevelFilter::Trace to TermLogger::init(LevelFilter::Warn I'm seeing only Warn and Error messages.

I'm also not sure about set_thread_mode which seems to behave a bit weird in regards to ThreadLogMode::Names:

fn main() {
    TermLogger::init(
        LevelFilter::Trace,
        ConfigBuilder::new()
            .set_thread_mode(ThreadLogMode::Names)
            .set_thread_level(LevelFilter::Error)
            .build(),
        TerminalMode::Mixed,
    )
    .unwrap();

    error!("This is a error from the main thread");

    let t = thread::Builder::new()
        .name("xxx".to_string())
        .spawn(move || {
            error!("This is a error");
        })
        .unwrap();

    t.join().unwrap();
}

This logs the thread IDs, but does not log the names. Using set_thread_level(LevelFilter::Trace) seems to have the same problem as the one described above.

Again, I'm not sure if this is a bug, or I'm simply misunderstanding the documentation.

Drakulix commented 3 years ago

Yeah you are right, this is confusing.

So what you would want is a more verbose output, the more verbose the log level is. I have touched this part of the code a few times to much to have ended up at this kinda broken state. I will fix this with 0.10.0 today.

Drakulix commented 3 years ago

After getting back to this, I am pretty sure, that this is expected behavior. As I said:

So what you would want is a more verbose output, the more verbose the log level is.

Which is what the current settings do. Reversing this for config options, leads to enabling more information for less frequent errors. I think the typical use case is not to log the file and line number only on errors and warnings, but on debug and trace statements.

So the documentation is off.

Drakulix commented 3 years ago

I hope d1e80a2c6669c28e9809dd7d952fdeee0da7fd5c makes that a little clearer. Feel free to reopen, if you have any better suggestions.

a-cristi commented 3 years ago

I think it's ok now. Thank you! :)