Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging Rust programs

Tags:

logging

rust

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?

like image 539
Daniel Fath Avatar asked Aug 25 '13 21:08

Daniel Fath


People also ask

What is logging in Rust?

The log create is the de-facto logging API in Rust. There are five log levels: error (highest priority), warn, info, debug, and trace (lowest priority). To log a message, you use the corresponding logging marcos: error!, warn!, etc. These marcos behave like println! and supports the syntax of format!.


2 Answers

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
like image 176
Chris Morgan Avatar answered Oct 05 '22 09:10

Chris Morgan


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

like image 40
Pawan Bisht Avatar answered Oct 05 '22 09:10

Pawan Bisht