7

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? Thank you!

Alen Siljak
  • 2,482
  • 2
  • 24
  • 29

1 Answers1

4

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

Handola
  • 56
  • 3
  • Thank you for the answer! That indeed solves my issue. Do you mind expanding just a bit on why this is happening? I would like to learn more (this may become my default choice for server-side code) and I'm sure it would be useful to others who may have the same question. – Alen Siljak Nov 04 '22 at 17:27
  • 1
    The filter you added didn't allow debug level traces to be emitted from this file, which is where the traces you want are coming from https://docs.rs/tower-http/latest/src/tower_http/trace/make_span.rs.html#13 – James Williams Nov 05 '22 at 09:30