New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Where's the most basic example for HTTP request logging? #296
Comments
|
All the things on TraceLayer are configurable so it should be possible to get that kind of output. You're welcome to contribute an example. tracing-subscriber requires setting RUST_LOG at least for some subscribers. Nothing tower-http can do about that. |
|
Thank you for your time. |
|
@gd87429 Doesn't get quite the same concise output that you'd see with axtic-web (as shown in your desired log format above) but the following will log requests in a fairly simple way without requiring use axum::{http::StatusCode, routing::get, Router};
use tokio;
use tower_http::trace::{self, TraceLayer};
use tracing::Level;
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let app = Router::new()
.route("/health", get(health))
.route("/error", get(error))
.layer(
TraceLayer::new_for_http()
.make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO))
.on_response(trace::DefaultOnResponse::new().level(Level::INFO)),
);
axum::Server::bind(&"127.0.0.1:8080".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}
async fn health() -> Result<&'static str, StatusCode> {
Ok("UP")
}
async fn error() -> Result<&'static str, StatusCode> {
Err(StatusCode::SERVICE_UNAVAILABLE)
}[dependencies]
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
axum = { version = "0.5" }
tower-http = { version = "0.3", features = ["trace"] }
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3" }The resulting logs: Changing the first line in tracing_subscriber::fmt()
.with_target(false)
.compact()
.init();Not quite what you're wanting, but HTH! |
|
@davidalger It does help, thank you for the detailed response. |
Feature Request
Motivation
It'd be nice to have a simple example that I can throw into axum/tower so the server log requests show up like this:
Currently, while checking out the example folder for axum:
RUST_LOGjust to see requests (and I'm not even sure which to use).Isn't there a one-liner I can use just to have some reasonably pretty request logging so I can see what endpoints have been called?
The text was updated successfully, but these errors were encountered: