From 0b9e7dff080b3fcf3907da343e17ab73012e7207 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 21 Aug 2022 18:46:54 +0200 Subject: Add TraceLayer --- src/main.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src') 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 -- cgit v1.2.3-18-g5258