Rocket - Logging to a file using log4rs
- Add log4rs to the dependencies.
examples/rocket/logging-with-log4rs-to-file/Cargo.toml
[package] name = "logging" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] log4rs = "1.3.0" rocket = "0.5"
- Create a configuration file:
examples/rocket/logging-with-log4rs-to-file/log4rs.yaml
appenders: stdout: kind: console all: kind: file path: "all.log" encoder: pattern: "{d} - {l} {f}:{L} {m}{n}" root: level: debug appenders: #- stdout - all
- Initiate the logging
examples/rocket/logging-with-log4rs-to-file/src/main.rs
#[macro_use] extern crate rocket; #[get("/")] fn index() -> &'static str { rocket::trace!("Trace from Hello World"); rocket::debug!("Debug from Hello World"); rocket::info!("Info from Hello World"); rocket::warn!("Warning from Hello World"); rocket::error!("Error from Hello World"); "Hello, world!" } #[launch] fn rocket() -> _ { log4rs::init_file("log4rs.yaml", Default::default()).unwrap(); rocket::build().mount("/", routes![index]) }