Skip to content

Commit

Permalink
Configured TraceLayer in web server to log info about request/response
Browse files Browse the repository at this point in the history
  • Loading branch information
mchf committed Apr 11, 2024
1 parent c5e1c5d commit 4badba9
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion rust/agama-server/src/web/service.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
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,
};
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.
///
Expand Down Expand Up @@ -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<Body>, _span: &Span| {
tracing::info!("request: {} {}", request.method(), request.uri().path())
})
.on_response(|response: &Response<Body>, latency: Duration, _span: &Span| {
tracing::info!("response: {} {:?}", response.status(), latency)
})
)
.layer(CompressionLayer::new().br(true))
.with_state(state)
}
Expand Down

0 comments on commit 4badba9

Please sign in to comment.