Skip to content

Latest commit

 

History

History
65 lines (40 loc) · 1.73 KB

rocket-logging.md

File metadata and controls

65 lines (40 loc) · 1.73 KB
title timestamp author published description tags
Rocket - logging in the web application
2024-01-18 10:50:01 -0800
szabgab
true
We can use println and eprintln in the Rocket-based code, but it is much better to use the logging facilities of Rocket. I think.
log
logging
debug!
info!
warn!
error!
Rocket
web

We can use println and eprintln in the Rocket-based code, but it is much better to use the logging facilities of Rocket. I think.

Dependencies

Nothing fancy. We use Rocket.

{% include file="examples/rocket/logging/Cargo.toml" %}

The code

This is just a plain Hello World! application with Rocket. I did not even include the tests.

{% include file="examples/rocket/logging/src/main.rs" %}

There are 4 macros we can use right from the rocket namespace:

rocket::debug!
rocket::info!
rocket::warn!
rocket::error!

We can run the application with cargo run and then when visit the web site at http://localhost:8000/ we will see the log messages on the console:

But oh, we only see the info, warn, and error messages. We don't see the debug message.

Configuring the log level

Rocket allows us to configure many things in the Rocket.toml file.

So I created it and added an entry to change the default normal level to debug.

{% include file="examples/rocket/logging/Rocket.toml" %}

Restarted the application and visited the web-site again.

This time I also saw the "debug" log message, but there was a lot of other output as Rocket printed both the whole Request and the whole Response struct.

I guess for now I stick with info!.