From 4badba9eb4620f71496756dd0585d7a78580c713 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Thu, 11 Apr 2024 07:48:02 +0200 Subject: [PATCH] Configured TraceLayer in web server to log info about request/response --- rust/agama-server/src/web/service.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/rust/agama-server/src/web/service.rs b/rust/agama-server/src/web/service.rs index 04300da332..a5eeb93179 100644 --- a/rust/agama-server/src/web/service.rs +++ b/rust/agama-server/src/web/service.rs @@ -1,9 +1,11 @@ use super::http::{login, login_from_query, logout, session}; use super::{auth::TokenClaims, config::ServiceConfig, state::ServiceState, EventsSender}; use axum::{ + body::Body, extract::Request, middleware, response::IntoResponse, + response::Response, routing::{get, post}, Router, }; @@ -11,8 +13,10 @@ use std::{ convert::Infallible, path::{Path, PathBuf}, }; +use std::time::Duration; use tower::Service; use tower_http::{compression::CompressionLayer, services::ServeDir, trace::TraceLayer}; +use tracing::Span; /// Builder for Agama main service. /// @@ -93,7 +97,14 @@ impl MainServiceBuilder { .route("/login", get(login_from_query)) .route("/po.js", get(super::http::po)) .nest("/api", api_router) - .layer(TraceLayer::new_for_http()) + .layer(TraceLayer::new_for_http() + .on_request(|request: &Request, _span: &Span| { + tracing::info!("request: {} {}", request.method(), request.uri().path()) + }) + .on_response(|response: &Response, latency: Duration, _span: &Span| { + tracing::info!("response: {} {:?}", response.status(), latency) + }) + ) .layer(CompressionLayer::new().br(true)) .with_state(state) }