Log crate info macro is not working

Im using a cargo workspace , and i'm planning to log::info macro instead of println macro, but its not displaying the output

use log::{info, trace, warn};

fn main() {

     let x = 10;
    // let y = test_async();
    println!("Hello boss");
    info!("Connected to port {} at {} Mb/s", x, x);

}

I dont know, why its not showing the inffo logs. I have also installed the log crate

You need to pick a logging implementation: log - Rust

1 Like

Hi @sachin6624,

Based on @sfackler's suggestion:

We could try the following:

Content of Cargo.toml
...
[dependencies]
log = "0.4" 
env_logger = "0.11"
Content of src/main.rs
use log::info;

fn main() {
    if std::env::var_os("RUST_LOG").is_none() {
        std::env::set_var("RUST_LOG", "debug");
    }

    env_logger::init();

    let x = 10;
    // let y = test_async();
    println!("Hello boss");
    info!("Connected to port {} at {} Mb/s", x, x);
}

Note we need to set the RUST_LOG environment variable. Please see env_logger example.

Or using an .env file:

Content of .env: 
RUST_LOG=debug
Content of Cargo.toml
...
[dependencies]
dotenv = "0.15"
log = "0.4" 
env_logger = "0.11"
Content of src/main.rs
use dotenv::dotenv;
use log::info;

fn main() {
    dotenv().ok();

    env_logger::init();

    let x = 10;
    // let y = test_async();
    println!("Hello boss");
    info!("Connected to port {} at {} Mb/s", x, x);
}

I have my own problem, too, at the moment with logging... They are so vast the documentation seem very "frugal" :rofl::rofl::rofl:

Best regards,

...behai.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.