From 1fab7958b38c374844b233afff88c9c5faa31900 Mon Sep 17 00:00:00 2001 From: Thani Sheun Date: Thu, 4 May 2023 19:00:42 +0200 Subject: [PATCH] Add tracing subscriber and remove redundant logger middleware --- Cargo.lock | 68 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 1 + src/middleware/logger.rs | 6 ---- src/middleware/mod.rs | 1 - src/routes/root.rs | 2 -- 6 files changed, 70 insertions(+), 9 deletions(-) delete mode 100644 src/middleware/logger.rs diff --git a/Cargo.lock b/Cargo.lock index 8802e9e..ddd4f4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -181,6 +181,7 @@ dependencies = [ "tokio", "tower", "tower-http", + "tracing-subscriber", "validator", ] @@ -1564,6 +1565,16 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" +[[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.3" @@ -1689,6 +1700,12 @@ version = "6.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ceedf44fb00f2d1984b0bc98102627ce622e083e49a5bacdb3e514fa4238e267" +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "parking_lot" version = "0.11.2" @@ -2247,6 +2264,15 @@ dependencies = [ "digest", ] +[[package]] +name = "sharded-slab" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" +dependencies = [ + "lazy_static", +] + [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -2568,6 +2594,16 @@ dependencies = [ "syn 2.0.15", ] +[[package]] +name = "thread_local" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "time" version = "0.1.45" @@ -2768,6 +2804,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" +dependencies = [ + "lazy_static", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", ] [[package]] @@ -2904,6 +2966,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "vcpkg" version = "0.2.15" diff --git a/Cargo.toml b/Cargo.toml index 778a00b..3d14aaf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,5 @@ sqlx = { version = "0.6.3", features = ["runtime-tokio-native-tls", "migrate", " validator = { version = "0.16.0", features = ["derive"] } async-trait = "0.1.68" sqlx-cli = "0.6.3" +tracing-subscriber = "0.3.17" diff --git a/src/main.rs b/src/main.rs index 6e6986a..ac8fa5d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,7 @@ async fn main() { .unwrap_or_else(|e| panic!("Database error: {}", e.to_string())); let host = format!("0.0.0.0:{}", parameter::get("PORT")); + tracing_subscriber::fmt::init(); axum::Server::bind(&host.parse().unwrap()) .serve(routes::root::routes(Arc::new(connection))) .await diff --git a/src/middleware/logger.rs b/src/middleware/logger.rs deleted file mode 100644 index 64d2253..0000000 --- a/src/middleware/logger.rs +++ /dev/null @@ -1,6 +0,0 @@ -use axum::{http::Request, middleware::Next, response::IntoResponse}; - -pub async fn logger(req: Request, next: Next) -> impl IntoResponse { - println!("{} {}", req.method(), req.uri()); - next.run(req).await -} diff --git a/src/middleware/mod.rs b/src/middleware/mod.rs index 3fb2789..0e4a05d 100644 --- a/src/middleware/mod.rs +++ b/src/middleware/mod.rs @@ -1,2 +1 @@ pub mod auth; -pub mod logger; diff --git a/src/routes/root.rs b/src/routes/root.rs index bff4dd5..9786a24 100644 --- a/src/routes/root.rs +++ b/src/routes/root.rs @@ -1,7 +1,6 @@ use super::auth; use crate::config::database::Database; use crate::middleware::auth as auth_middleware; -use crate::middleware::logger; use crate::routes::{profile, register}; use crate::state::auth_state::AuthState; use crate::state::token_state::TokenState; @@ -29,7 +28,6 @@ pub fn routes(db_conn: Arc) -> IntoMakeService { let app_router = Router::new() .nest("/api", merged_router) - .layer(ServiceBuilder::new().layer(middleware::from_fn(logger::logger))) .layer(TraceLayer::new_for_http()); app_router.into_make_service()