Unable to get tracing macros to compile

I'm trying to get set up using the tracing framework following the instructions in their documentation for recording fields:


I included the dependency in my Cargo.toml:

tracing = "0.1.29"

and then tried to use the macros in main.rs:

use tracing::info;
fn main() {
    let number = 42;
    info!("Running Main", number, value = 7);
    println!("Hello, world!");

but this results in a compile error:

 --> src/main.rs:5:27
5 |     info!("Running Main", number, value = 7);
  |           --------------  ^^^^^^          ^ named argument never used
  |           |               |
  |           |               argument never used
  |           multiple missing formatting specifiers

error: could not compile `testtracing` due to previous error

I considered submitting a bug on github, but I feel that it's unlikely the core functionality of a very popular rust library would be broken, so I assume I am doing something obviously wrong here? Thanks!

You may need to put the field names before the string:

info!(%number, value = 7, "Running Main");
1 Like

I'm not familiar with tracing yet, but this sentence from the documentation seems relevant:

Finally, events may also include human-readable messages, in the form of a format string and (optional) arguments, after the event’s key-value fields.

So, you need to either use the values as format arguments (put {}s in the string), or put the values before the string to use them as key-value fields. Which one to do depends on what you want logged.

1 Like

Ah you are totally correct @Yandros, the arguments go before the message for event! macros. Quite surprising since the tracing span! macro is exactly the opposite, where you do span!(Level::TRACE, "login", user);

If I understand correctly, span! can take message too - the valid syntax would be span!(Level::TRACE, "span_name", field, "message: {}", message_arg);

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.