In Rust, the debug! macro from the log crate is used for logging debug messages. By default, it logs to stderr, not stdout. However, you can configure the logging backend to redirect logs to stdout instead.
debug! to stdout on Linuxenv_logger CrateThe simplest way is to use the env_logger crate, which allows you to configure logging via environment variables.
Add env_logger and log to your Cargo.toml:
toml[dependencies] log = "0.4" env_logger = "0.10"
In your code, initialize the logger and set the default target to stdout:
rustuse log::debug; fn main() { // Initialize the logger env_logger::Builder::new() .target(env_logger::Target::Stdout) .init(); debug!("This debug message will go to stdout"); }
Run your program with the RUST_LOG environment variable to enable debug logs:
shRUST_LOG=debug cargo run
stdoutIf you want more control, you can implement a custom logger using the log crate and redirect logs to stdout:
rustuse log::{debug, Level, LevelFilter, Metadata, Record}; use std::io::Write; struct StdoutLogger; impl log::Log for StdoutLogger { fn enabled(&self, metadata: &Metadata) -> bool { metadata.level() <= Level::Debug } fn log(&self, record: &Record) { if self.enabled(record.metadata()) { println!("[{}] {}", record.level(), record.args()); } } fn flush(&self) {} } static LOGGER: StdoutLogger = StdoutLogger; fn main() { log::set_boxed_logger(Box::new(LOGGER)).unwrap(); log::set_max_level(LevelFilter::Debug); debug!("This debug message will go to stdout"); }
simple_loggerThe simple_logger crate also allows you to log to stdout:
rustuse log::debug; use simple_logger::SimpleLogger; fn main() { SimpleLogger::new() .with_level(log::LevelFilter::Debug) .with_target(simple_logger::Target::Stdout) .init() .unwrap(); debug!("This debug message will go to stdout"); }
env_logger for easy configuration via environment variables.simple_logger for more control.debug or lower.