So, just thought of doing a simple proof of concept to see how my experience goes.
As a first step, created a new app using cargo
$ cargo new --bin log_sample
This creates a folder log_sample in my current folder.
Now, modified main.rs to look like below..
Basically, added info! and error! calls to see the output, using log and env_logger crates.
Now, if i run the application,
$ cargo run Running `target/debug/log_sample` ERROR:log_sample: error Hello, world!
I don’t see “starting up!” appearing here. I tried to run using the app..
$ ./target/debug/log_sample ERROR:log_sample: error Hello, world!
This also do not show up the “starting up”. Is there is something wrong? Not really. By default, it is logging only errors, which makes sense. How do I turn on the info logging also? Instructions from env_logger documentation is clear enough, and I tried the following.
$ RUST_LOG=info ./target/debug/log_sample INFO:log_sample: starting up ERROR:log_sample: error Hello, world!
There we go!! I do see the “starting up” now. So, the environment variable RUST_LOG is controlling what kind of data to log. Once I turned on info log using RUST_LOG=info, the info! calls also shows its output.
So, that was a cool abstraction for logging. Pretty good indeed!!