19

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?

asked Aug 25, 2013 at 21:21

4 Answers 4

13

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
answered Aug 25, 2013 at 21:57
Sign up to request clarification or add additional context in comments.

4 Comments

debug requires compiling the program with --cfg debug too: they get entirely removed without it.
I tried to edit your question dbaupps suggestion, but it seems to have been rejected. could you change that snippet above to set RUST_LOG=myprog=4\n rustc myprog.rs --cfg debug\n myprog.exe?
Recently, --cfg=debug isn't needed, this is on by default, and if you odn't want it, you need --cfg=nodebug
@SteveKlabnik Actually, it's --cfg ndebug
8

If 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 ->

enter image description here

answered Aug 19, 2021 at 13:19

1 Comment

FYI: within CLion an Environment variables entry of RUST_LOG="debug" does NOT work. RUST_LOG=debug works as expected.
3

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().

rustyMagnet
4,2853 gold badges42 silver badges50 bronze badges
answered Sep 23, 2019 at 10:28

Comments

3

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.

Here are the log docs.

answered Dec 14, 2023 at 21:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.