Providing logging
In a big application, things will sooner or later not go as planned. But that's okay, as long as you have provided a system for your users to know what went wrong and, if possible, why. One time-tested tool to accomplish this is detailed logs that let the user specify for themselves how much diagnosis they want to see.
How to do it...
Follow these steps:
Open the
Cargo.toml
file that has been generated earlier for you.Under
[dependencies]
, add the following line:
log = "0.4.1" env_logger = "0.5.3"
- If you want, you can go to log's (https://crates.io/crates/log) or
env_logger
's (https://crates.io/crates/env_log) crates.io pages to check for the newest version and use that one instead. - In the folder
bin
, create a file calledlogging.rs
. Add the following code and run it with
RUST_LOG=logging cargo run --bin logging
if you're on a Unix-based system. Otherwise, run$env:RUST_LOG="logging"; cargo run --bin logging
on Windows:
1 extern crate env_logger; 2 #[macro_use] 3 extern crate...