tracing-aka-logging help #689
-
I am learning to use axum
But I copied this example into my own project, but I can’t print this sentence. My code is exactly the same as the code in tracing-aka-logging.
This code cannot be printed to the console normally, I am confused ,Can you help me? Below is the complete code and dependencies there is only one main.rs file use axum::{
body::Bytes,
http::{HeaderMap, Request},
response::{Html, Response},
routing::get,
Router,
};
use std::{net::SocketAddr, time::Duration};
use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer};
use tracing::Span;
#[tokio::main]
async fn main() {
// Set the RUST_LOG, if it hasn't been explicitly defined
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var(
"RUST_LOG",
"example_tracing_aka_logging=debug,tower_http=debug",
)
}
tracing_subscriber::fmt::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.
.layer(TraceLayer::new_for_http())
// If you want to customize the behavior using closures here is how
//
// This is just for demonstration, you don't need to add this middleware twice
.layer(
TraceLayer::new_for_http()
.on_request(|_request: &Request<_>, _span: &Span| {
// ...
})
.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);
tracing::info!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn handler() -> Html<&'static str> {
tracing::debug!("hello world");
Html("<h1>Hello, World!</h1>")
}
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Since your crate has a different name you need to update the log filter: std::env::set_var(
"RUST_LOG",
- "example_tracing_aka_logging=debug,tower_http=debug",
+ "glimmer=debug,tower_http=debug",
) |
Beta Was this translation helpful? Give feedback.
-
Hey @davidpdrsn, I came across this issue after running into the same problem. The solution you provided works, but I'd like to know where in the documentation I can find a description of an acceptable log filter configuration. I've already looked through https://github.com/tokio-rs/tower-http repo, and although the examples in that repo do use the crate's name in the log filter it didn't register to me that it had to be the crate's name, so I changed it to something else and saw that the log levels didn't change as I expected. |
Beta Was this translation helpful? Give feedback.
Since your crate has a different name you need to update the log filter: