mirror of
https://github.com/tokio-rs/axum.git
synced 2024-12-29 15:49:16 +01:00
Add dedicated tracing/logging example (#155)
Useful to link to since several have asked.
This commit is contained in:
parent
85bb0158be
commit
2389761ce7
2 changed files with 38 additions and 1 deletions
|
@ -18,7 +18,7 @@
|
||||||
- [`chat`](../examples/chat.rs) - Chat application example.
|
- [`chat`](../examples/chat.rs) - Chat application example.
|
||||||
- [`404`](../examples/404.rs) - Custom 404 page.
|
- [`404`](../examples/404.rs) - Custom 404 page.
|
||||||
- [`async-graphql`](../examples/async-graphql) - GraphQL example using [`async-graphql`](https://crates.io/crates/async-graphql).
|
- [`async-graphql`](../examples/async-graphql) - GraphQL example using [`async-graphql`](https://crates.io/crates/async-graphql).
|
||||||
|
- [`tracing_aka_logging`](../examples/tracing_aka_logging) - How to setup and configure tracing/logging.
|
||||||
|
|
||||||
# Community showcase
|
# Community showcase
|
||||||
|
|
||||||
|
|
37
examples/tracing_aka_logging.rs
Normal file
37
examples/tracing_aka_logging.rs
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
//! Run with
|
||||||
|
//!
|
||||||
|
//! ```not_rust
|
||||||
|
//! cargo run --example tracing_aka_logging
|
||||||
|
//! ```
|
||||||
|
|
||||||
|
use axum::prelude::*;
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use tower_http::trace::TraceLayer;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
// Set the RUST_LOG, if it hasn't been explicitly defined
|
||||||
|
if std::env::var("RUST_LOG").is_err() {
|
||||||
|
std::env::set_var("RUST_LOG", "tracing_aka_logging=debug,tower_http=debug")
|
||||||
|
}
|
||||||
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
|
// build our application with a route
|
||||||
|
let app = 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.
|
||||||
|
.layer(TraceLayer::new_for_http());
|
||||||
|
|
||||||
|
// 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() -> response::Html<&'static str> {
|
||||||
|
response::Html("<h1>Hello, World!</h1>")
|
||||||
|
}
|
Loading…
Reference in a new issue