From eef33cdf0cfc639eb3f9f7a5226856dbe3aa199a Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sun, 2 Oct 2022 16:31:58 -0400 Subject: [PATCH] Switch to fern logger It was annoying to get debug logs for modules that aren't even ours --- Cargo.lock | 45 ++++++++++++++++++++++++++++++++++++++++----- Cargo.toml | 2 ++ src/main.rs | 23 ++++++++++++++++++++--- 3 files changed, 62 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ad85934..634c18d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,15 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "250f629c0161ad8107cf89319e990051fae62832fd343083bea452d93e2205fd" +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "async-channel" version = "1.6.1" @@ -267,14 +276,16 @@ dependencies = [ [[package]] name = "chrono" -version = "0.4.19" +version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +checksum = "bfd4d1b31faaa3a89d7934dbded3111da0d2ef28e3ebccdb4f0179f5929d1ef1" dependencies = [ - "libc", + "iana-time-zone", + "js-sys", "num-integer", "num-traits", "time 0.1.44", + "wasm-bindgen", "winapi", ] @@ -447,6 +458,15 @@ dependencies = [ "instant", ] +[[package]] +name = "fern" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bdd7b0849075e79ee9a1836df22c717d1eba30451796fdc631b04565dd11e2a" +dependencies = [ + "log", +] + [[package]] name = "foreign-types" version = "0.3.2" @@ -673,6 +693,19 @@ dependencies = [ "syn", ] +[[package]] +name = "iana-time-zone" +version = "0.1.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd911b35d940d2bd0bea0f9100068e5b97b51a1cbe13d13382f132e0365257a0" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "js-sys", + "wasm-bindgen", + "winapi", +] + [[package]] name = "idna" version = "0.2.3" @@ -759,9 +792,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.125" +version = "0.2.134" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b" +checksum = "329c933548736bc49fd575ee68c89e8be4d260064184389a5b77517cddd99ffb" [[package]] name = "linked-hash-map" @@ -2032,7 +2065,9 @@ dependencies = [ "async-native-tls", "async-std", "async-trait", + "chrono", "derive-getters", + "fern", "futures", "futures-timer", "itertools", diff --git a/Cargo.toml b/Cargo.toml index 1fc61f3..c0f2def 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,8 @@ simplelog = "0.12.0" async-trait = "0.1.53" scraper = "0.13.0" itertools = "0.10.4" +fern = "0.6.1" +chrono = "0.4.22" # For annoying reasons, we must pin exactly the same versions as async-imap if we want to use # their types. diff --git a/src/main.rs b/src/main.rs index 3f68186..7b8a269 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,6 @@ extern crate log; use futures::{stream::StreamExt, Future}; use log::LevelFilter; -use simplelog::{Config as LogConfig, SimpleLogger}; -use std::collections::HashMap; use std::{fs::File, sync::Arc}; use tokio::runtime::Runtime; use ynabifier::parse::{Transaction, TransactionEmailParser}; @@ -16,7 +14,7 @@ use ynabifier::{ }; fn main() { - SimpleLogger::init(LevelFilter::Debug, LogConfig::default()).expect("setup failed"); + setup_logger().expect("failed to seutp logger"); let config_file = File::open("config.yml").expect("failed to open config file"); let config = serde_yaml::from_reader::<_, Config>(config_file).expect("failed to parse config file"); @@ -50,6 +48,25 @@ fn main() { }); } +fn setup_logger() -> Result<(), fern::InitError> { + fern::Dispatch::new() + .format(|out, message, record| { + out.finish(format_args!( + "{} [{}] [{}] {}", + chrono::Local::now().format("%Y-%m-%dT%H:%M:%S"), + record.level(), + record.target(), + message + )) + }) + .level(LevelFilter::Info) + .level_for("ynabifier", LevelFilter::Debug) + .chain(std::io::stderr()) + .apply()?; + + Ok(()) +} + fn try_parse_email<'a, I>(parser_iter: I, msg: &Message) -> Option where I: Iterator)>,