diff options
author | Santo Cariotti <santo@dcariotti.me> | 2022-08-21 18:46:54 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2022-08-21 18:46:54 +0200 |
commit | 0b9e7dff080b3fcf3907da343e17ab73012e7207 (patch) | |
tree | dfffd9cb602b2e4ca6db9970d6fef93a14c00c9e /src | |
parent | 66e677a8543285ed424d616092fb09c518b32fa4 (diff) |
Add TraceLayer
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs index ab463b8..810e202 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,16 @@ mod errors; mod logger; -use axum::{http::header, routing::get, Json, Router}; +use axum::{ + http::{header, Request}, + routing::get, + Json, Router, +}; use serde::Serialize; +use std::time::Duration; use tower_http::sensitive_headers::SetSensitiveHeadersLayer; +use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer}; +use tracing::Span; #[tokio::main] async fn main() { @@ -29,6 +36,17 @@ async fn create_app() -> Router { .layer(SetSensitiveHeadersLayer::new(std::iter::once( header::AUTHORIZATION, ))) + .layer( + TraceLayer::new_for_http() + .on_request(|request: &Request<_>, _span: &Span| { + tracing::info!("{} {}", request.method(), request.uri()); + }) + .on_failure( + |error: ServerErrorsFailureClass, latency: Duration, _span: &Span| { + tracing::error!("{} | {} s", error, latency.as_secs()); + }, + ), + ) } // Example root which says hi |