If we have following code
fn main() {
error!("This is an error log")
warn!("This is a warn log")
info!("this is an info log")
debug!("This is a debug log")
}
How do we enable the debug level output on Windows?
4 Answers 4
When executing your program, you need to set the RUST_LOG environment variable appropriately; it is (as far as this is concerned) a comma-separated key=value list; the keys are crate or module names, e.g. extra or std::option; the values are numbers, mapped to log levels:
- 1: error
- 2: warn
- 3: info
- 4: debug
(Each level includes the more significant levels.)
In Command Prompt, compiling and running myprog with showing warnings and errors would be something like:
rustc myprog.rs
set RUST_LOG=myprog=4
myprog.exe
4 Comments
debug requires compiling the program with --cfg debug too: they get entirely removed without it.RUST_LOG=myprog=4\n rustc myprog.rs --cfg debug\n myprog.exe?--cfg=debug isn't needed, this is on by default, and if you odn't want it, you need --cfg=nodebug--cfg ndebugIf you look at env_logger documentation it says:
"by default all logging is disabled except for the error level."
You set an ENVIRONMENT variable. In your case: RUST_LOG=debug.
extern crate env_logger;
extern crate log;
use log::{debug, error, info, warn};
fn main() {
env_logger::init();
error!("[!] error");
warn!("[!] warn");
info!("[!] info");
debug!("[!] debug");
}
/*
[2021年08月19日T13:14:15Z ERROR cliapp] [!] error
[2021年08月19日T13:14:15Z WARN cliapp] [!] warn
[2021年08月19日T13:14:15Z INFO cliapp] [!] info
[2021年08月19日T13:14:15Z DEBUG cliapp] [!] debug
*/
CLion tip ->
1 Comment
Environment variables entry of RUST_LOG="debug" does NOT work. RUST_LOG=debug works as expected.You can set the logging level in the program by setting them to your environment as well. The statement you have to write is:
RUST_LOG=YOUR-PROJECT-NAME=log_level
eg:
RUST_LOG=Hello-World=info or RUST_LOG=Hello-World=3
After setting your log level the next step is to initialize them by using env_logger::init().
Comments
I hope this is helpful for people. Below is my solution for using log. I wanted to turn on by default, instead of controlling log via environment variables.
Add this code before you issue log statements.
// enable logging, since log defaults to silent
std::env::set_var("RUST_LOG", "info");
env_logger::init();
...
debug!("LOGGING **************************");
info!("LOGGING **************************");
error!("LOGGING **************************");
Then, all of these will output to stdout.