8

Learning Axum and I will like to add logging to a service I have put together, but unfortunately I cannot get it to work.

What I have done?

I added tower-http = { version = "0.3.5", features = ["trace"] } to Cargo.html and in the definition of the service I have this:

use tower_http::trace::TraceLayer;

let app = Router::new()
            .route("/:name/path", axum::routing::get(handler))
            .layer(TraceLayer::new_for_http())

But when I start the application and make requests to the endpoint, nothing gets logged.

Is there any configuration step I could have missed?

** Edit 1 **

As suggested in the comment, I added the tracing-subscriber crate:

tracing-subscriber = { version = "0.3"}

and updated the code as follows:

use tower_http::trace::TraceLayer;
use tower_http::trace::TraceLayer;

tracing_subscriber::fmt().init();
let app = Router::new()
            .route("/:name/path", axum::routing::get(handler))
            .layer(TraceLayer::new_for_http())

But yet, still no log output.

** Edit 2 **

Ok so what finally worked was

use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

and then initilize as follows:

        tracing_subscriber::registry()
            .with(tracing_subscriber::fmt::layer())
            .init();

Even though this get the log output, I cannot explain why. So perhaps someone who understands how these crates work can give an explanation which I can accept as the answer to the question

Finlay Weber
  • 2,989
  • 3
  • 17
  • 37
  • Have you added a subscriber? A simple start to using tracing is to use the tracing-subscriber crate and add `tracing_subscriber::fmt().init();` See the info here: [How to use the tracing library?](/q/70013172/2189130) – kmdreko Jan 04 '23 at 18:17
  • @kmdreko I wasn't before, but now I added it, and yet there is still no log output – Finlay Weber Jan 04 '23 at 18:43

1 Answers1

8

You can get up and running quickly with the tracing-subscriber crate:

tracing_subscriber::fmt()
    .with_max_level(tracing::Level::DEBUG)
    .init();

The difference in the above attempts is simply a case of defaults. By default, TraceLayer will log with a DEBUG level. Using fmt() is configured with a default INFO logging level while registry().with(..).init() does not configure a log level filter.

You can also change the behavior of TraceLayer by using the customizable on_* methods.

See also:

kmdreko
  • 42,554
  • 6
  • 57
  • 106
  • What if you want the user to be able to configure the level of the logs? How can that be done? The examples above looks like it decides what the log level will be directly in the code and that may not be desirable – Finlay Weber Jan 04 '23 at 20:34
  • @FinlayWeber Sounds like you may be interested in [`EnvFilter`](https://docs.rs/tracing-subscriber/0.3.16/tracing_subscriber/struct.EnvFilter.html)/[`.with_env_filter()`](https://docs.rs/tracing-subscriber/0.3.16/tracing_subscriber/fmt/struct.SubscriberBuilder.html#method.with_env_filter) so the logging level can be customized by an environment variable. And its more customizable than just globally. – kmdreko Jan 04 '23 at 20:41
  • 1
    Thanks! I just have to say that it is not the best that the bits needed to enable and configure logging are all scattered across different crates, with no central place to read up on what is needed. – Finlay Weber Jan 04 '23 at 21:18