From: Martin Pitt Date: Sat, 25 Sep 2021 10:42:02 +0000 (+0200) Subject: serde: Use log framework X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=commitdiff_plain;h=33b9630d43871bea43f39d3bbc455db1a5bdab7a serde: Use log framework Run with `RUST_LOG=debug` to see the serialized message. --- diff --git a/serde/Cargo.toml b/serde/Cargo.toml index 64d55f5..b5dbc41 100644 --- a/serde/Cargo.toml +++ b/serde/Cargo.toml @@ -8,3 +8,5 @@ edition = "2018" [dependencies] serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" +log = "0.4" +env_logger = "0.9" diff --git a/serde/src/main.rs b/serde/src/main.rs index 8579522..fc88d23 100644 --- a/serde/src/main.rs +++ b/serde/src/main.rs @@ -1,5 +1,6 @@ use std::fs; use std::error::Error; + use serde::{Serialize, Deserialize}; const DB_PATH: &str = "/tmp/contacts.json"; @@ -31,7 +32,7 @@ fn build_contacts() -> Contacts { fn create_contacts() -> Result<(), Box> { let contacts = build_contacts(); let serialized = serde_json::to_string(&contacts)?; - println!("serialized: {}", serialized); + log::debug!("serialized: {}", &serialized); let mut f = fs::File::create(DB_PATH)?; serde_json::to_writer_pretty(&mut f, &contacts)?; Ok(()) @@ -43,8 +44,9 @@ fn load_contacts() -> Result> { } fn main() -> Result<(), Box> { + env_logger::init(); create_contacts()?; let contacts = load_contacts()?; - println!("deserialized: {:?}", contacts); + println!("deserialized: {:?}", &contacts); Ok(()) }