mirror of
https://github.com/tokio-rs/axum.git
synced 2025-03-24 15:59:17 +01:00
93 lines
3.4 KiB
Rust
93 lines
3.4 KiB
Rust
//! Run with
|
|
//!
|
|
//! ```not_rust
|
|
//! cargo run -p example-tracing-aka-logging
|
|
//! ```
|
|
|
|
use axum::{
|
|
body::Bytes,
|
|
extract::MatchedPath,
|
|
http::{HeaderMap, Request},
|
|
response::{Html, Response},
|
|
routing::get,
|
|
Router,
|
|
};
|
|
use std::{net::SocketAddr, time::Duration};
|
|
use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer};
|
|
use tracing::{info_span, Span};
|
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
tracing_subscriber::registry()
|
|
.with(
|
|
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
|
|
// axum logs rejections from built-in extractors with the `axum::rejection`
|
|
// target, at `TRACE` level. `axum::rejection=trace` enables showing those events
|
|
"example_tracing_aka_logging=debug,tower_http=debug,axum::rejection=trace".into()
|
|
}),
|
|
)
|
|
.with(tracing_subscriber::fmt::layer())
|
|
.init();
|
|
|
|
// build our application with a route
|
|
let app = Router::new()
|
|
.route("/", get(handler))
|
|
// `TraceLayer` is provided by tower-http so you have to add that as a dependency.
|
|
// It provides good defaults but is also very customizable.
|
|
//
|
|
// See https://docs.rs/tower-http/0.1.1/tower_http/trace/index.html for more details.
|
|
//
|
|
// If you want to customize the behavior using closures here is how.
|
|
.layer(
|
|
TraceLayer::new_for_http()
|
|
.make_span_with(|request: &Request<_>| {
|
|
// Log the matched route's path (with placeholders not filled in).
|
|
// Use request.uri() or OriginalUri if you want the real path.
|
|
let matched_path = request
|
|
.extensions()
|
|
.get::<MatchedPath>()
|
|
.map(MatchedPath::as_str);
|
|
|
|
info_span!(
|
|
"http_request",
|
|
method = ?request.method(),
|
|
matched_path,
|
|
some_other_field = tracing::field::Empty,
|
|
)
|
|
})
|
|
.on_request(|_request: &Request<_>, _span: &Span| {
|
|
// You can use `_span.record("some_other_field", value)` in one of these
|
|
// closures to attach a value to the initially empty field in the info_span
|
|
// created above.
|
|
})
|
|
.on_response(|_response: &Response, _latency: Duration, _span: &Span| {
|
|
// ...
|
|
})
|
|
.on_body_chunk(|_chunk: &Bytes, _latency: Duration, _span: &Span| {
|
|
// ...
|
|
})
|
|
.on_eos(
|
|
|_trailers: Option<&HeaderMap>, _stream_duration: Duration, _span: &Span| {
|
|
// ...
|
|
},
|
|
)
|
|
.on_failure(
|
|
|_error: ServerErrorsFailureClass, _latency: Duration, _span: &Span| {
|
|
// ...
|
|
},
|
|
),
|
|
);
|
|
|
|
// run it
|
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
|
tracing::debug!("listening on {}", addr);
|
|
axum::Server::bind(&addr)
|
|
.serve(app.into_make_service())
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
async fn handler() -> Html<&'static str> {
|
|
Html("<h1>Hello, World!</h1>")
|
|
}
|