From f044b1833760ef450bbcb8065f63ec9929d23d64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=80=E1=B4=8D=E1=B4=9B=E1=B4=8F=E1=B4=80=E1=B4=87?= =?UTF-8?q?=CA=80?= Date: Thu, 2 May 2024 03:00:16 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BD=BF=E7=94=A8=20tracing=20?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=20env=5Flogger=20(#93)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 70 ++++++++++++++++++++++++++++++----------------------- Cargo.toml | 3 ++- src/main.rs | 14 ++++++++--- 3 files changed, 53 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5b38371..be38709 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -427,7 +427,6 @@ dependencies = [ "cookie 0.18.1", "dirs", "entity", - "env_logger", "filenamify", "float-ord", "futures", @@ -450,6 +449,8 @@ dependencies = [ "thiserror", "tokio", "toml", + "tracing", + "tracing-subscriber", ] [[package]] @@ -862,29 +863,6 @@ dependencies = [ "serde_json", ] -[[package]] -name = "env_filter" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea" -dependencies = [ - "log", - "regex", -] - -[[package]] -name = "env_logger" -version = "0.11.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9" -dependencies = [ - "anstream", - "anstyle", - "env_filter", - "humantime", - "log", -] - [[package]] name = "equivalent" version = "1.0.1" @@ -1366,12 +1344,6 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" -[[package]] -name = "humantime" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" - [[package]] name = "hyper" version = "1.2.0" @@ -1700,6 +1672,16 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num-bigint" version = "0.4.4" @@ -1828,6 +1810,12 @@ dependencies = [ "syn 2.0.58", ] +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "parking" version = "2.2.0" @@ -3446,6 +3434,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", ] [[package]] @@ -3454,13 +3454,17 @@ version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" dependencies = [ + "chrono", "matchers", + "nu-ansi-term", "once_cell", "regex", "sharded-slab", + "smallvec", "thread_local", "tracing", "tracing-core", + "tracing-log", ] [[package]] @@ -3552,6 +3556,12 @@ dependencies = [ "serde", ] +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "value-bag" version = "1.8.1" diff --git a/Cargo.toml b/Cargo.toml index 5ab29f4..683d1b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,6 @@ chrono = { version = "0.4.35", features = ["serde"] } cookie = "0.18.0" dirs = "5.0.1" entity = { path = "entity" } -env_logger = "0.11.3" filenamify = "0.1.0" float-ord = "0.3.2" futures = "0.3.30" @@ -49,6 +48,8 @@ strum = { version = "0.26", features = ["derive"] } thiserror = "1.0.58" tokio = { version = "1", features = ["full"] } toml = "0.8.12" +tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["chrono"] } [workspace] members = [".", "entity", "migration"] diff --git a/src/main.rs b/src/main.rs index ecde679..1433821 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ #[macro_use] -extern crate log; +extern crate tracing; mod bilibili; mod config; @@ -8,8 +8,8 @@ mod database; mod downloader; mod error; -use env_logger::Env; use once_cell::sync::Lazy; +use tracing_subscriber::util::SubscriberInitExt; use crate::bilibili::BiliClient; use crate::config::CONFIG; @@ -18,7 +18,15 @@ use crate::database::{database_connection, migrate_database}; #[tokio::main] async fn main() -> ! { - env_logger::init_from_env(Env::default().default_filter_or("None,bili_sync=info")); + let default_log_level = std::env::var("RUST_LOG").unwrap_or("None,bili_sync=info".to_owned()); + tracing_subscriber::fmt::Subscriber::builder() + .with_env_filter(tracing_subscriber::EnvFilter::builder().parse_lossy(default_log_level)) + .with_timer(tracing_subscriber::fmt::time::ChronoLocal::new( + "%Y-%m-%d %H:%M:%S%.3f".to_owned(), + )) + .finish() + .try_init() + .expect("初始化日志失败"); Lazy::force(&SCAN_ONLY); Lazy::force(&CONFIG); let mut anchor = chrono::Local::now().date_naive();