rustrust-axumrust-tracing

How to properly filter request logs with Axum?


I am using Axum for a relatively simple Web API and would like to get a logging/tracing output for incoming requests similar to Go Gin, IIS logs, Python FastAPI, etc. - a simple path and parameters output.

The layer for HTTP is added to the Router:

let app = Router::new()
    .route("/hello", get(hello_img))
    .layer(TraceLayer::new_for_http());

This results in approximately what I'm looking for: good_output

However, there is also a lot of additional unwanted logging going on so I am adding a filter to tune these out. After adding a filter:

let filter = filter::Targets::new()
    .with_target("tower_http::trace::on_response", Level::TRACE)
    .with_target("tower_http::trace::on_request", Level::TRACE)
    .with_default(Level::INFO);

and adding it to the subscriber:

let tracing_layer = tracing_subscriber::fmt::layer();

tracing_subscriber::registry()
    .with(tracing_layer)
    .with(filter)
    .init();

the output changes to bad_output

The details (method, URI, parameters) are gone.

Why is this happening even though no formatting change was specified? How to keep the request/response tracing in the console but filtering out other unwanted traces?


Solution

  • Your filter currently filters make_span. Add this to your targets:

    .with_target("tower_http::trace::make_span", Level::DEBUG)
    

    Edit: Level::DEBUG should be enough for your targets