Drakulix / simplelog.rs

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

Usage question #96

Closed MostHated closed 2 years ago

MostHated commented 2 years ago

Hey there, I apologize, as I realize that this is probably a silly question, but this is still my first week using Rust, so some things aren't quite as obvious to me yet compared to other languages. I making a tool which has a workspace containing one library crate that is shared between two binary crates. I was considering putting the logger in the library so that both binaries could grab it and use it, but I am honestly not sure how I might go about this after having implemented it following the example.

With this example:

    CombinedLogger::init(vec![
        TermLogger::new(LevelFilter::Warn, simplelog::Config::default(), TerminalMode::Mixed, ColorChoice::Auto),
        WriteLogger::new(LevelFilter::Info, simplelog::Config::default(), File::create("my_rust_binary.log").unwrap()),
    ])
    .unwrap();

    error!("Bright red error");
    info!("This only appears in the log file");
    debug!("This level is currently not enabled for any logger");

I am not sure exactly what I could store and reference from the binary crates. From other languages, I am used to being able to instantiate some sort of static object and reference that in order to use it, but it looks like the logger gets initialized and constructed, then simply used without returning any sort of reference that I could then grab from another crate or module? I am sure I am just missing something fairly obvious, but would it be possible to simply be pointed in the right direction on what a common solution might be for this?

Thanks, I definitely appreciate it, -MH

MostHated commented 2 years ago

Actually, I was able to go through a few examples from other repos and get it worked out. Sorry to bother!