I've got some route handlers with print!()
statements in it. They work just fine when I compile my app for debug. However, when compiling for release, Rocket appears to be "muting" all the print statements (as well as logging functions from log
crate). Why is it doing so? How do I force it to print things then building for release? I suspect it might be connected to log level, which is automatically set to critical when compiling for release, however, I'm not quite sure what level do print!
statements have and how to change the logging level.
Asked
Active
Viewed 1,215 times
3

keddad
- 1,398
- 3
- 14
- 35
-
I don't remember tracing can capture `print` statement. Can you provide an example of what you get ? – Stargateur Aug 19 '21 at 20:51
-
4You are specifically mentioning `print!()`, which does not [`flush()`](https://doc.rust-lang.org/stable/std/io/trait.Write.html#tymethod.flush) `stdout` after writing to it, so its output usually appears to be muted. As you mention, `rocket` sets the [default log level](https://rocket.rs/v0.5-rc/guide/configuration/#overview) to `normal` in debug builds, which will produce more/any logs, which in turn cause a `flush()` and therefore your `print()`-output to be flushed as well. Use `println!()` or manually `flush()`. – user2722968 Aug 19 '21 at 20:51
-
@user2722968 that worked, thanks! Would you like to add this as an answer, so I could accept it? – keddad Aug 20 '21 at 10:22
-
@keddad added the answer so this can be closed. – user2722968 Aug 20 '21 at 10:32
2 Answers
3
You are specifically mentioning print!()
, which does not flush()
stdout
after writing to it, so its output usually appears to be muted.
As you mention, rocket
sets the default log-level to normal
in debug builds, which will produce more/any logs, which in turn cause a flush()
and therefore your print!()
-output to be flushed as well.
Use println!()
or manually flush()
.

user2722968
- 13,636
- 2
- 46
- 67
0
I just want to add a quick reference for How to flush
after print
In Rust, the print!
macro is buffered by default, which means that the output may not be immediately visible. To flush the output buffer and ensure that your print!
statements are immediately visible, you can use the std::io::stdout()
function and call its flush()
method.
use std::io::{self, Write};
fn main() {
print!("Hello, world!");
io::stdout().flush().unwrap(); // flushes the output buffer
}

Swaroop Maddu
- 4,289
- 2
- 26
- 38