I'm bad at macros, really bad

Hi,

I'm trying to do a simple macro (well I though it would be simple):

but it's failing and I don't have the slight idea on what's going on :-/

Thanks for any insight

It works fine like it is, you just forgot to actually use the argument in the message. Try

fn main() {
    let value = 42;
    info!("message {key}", key = value);
    //             ^^^^^ this part
}

At least this way it compiles for me.

Edit: If you wanted to pass one of the key/value pairs that aren't part of the actual message, I believe those come before the format string, but it's been a while since I used tracing.

1 Like

Thank you for your answer :slight_smile:

I don't get it: What is this "{key}"?

formatting docs

1 Like

Some additional context to what @H2CO3 pointed out quicker than me: The tracing::event! macro delegates to the same string formatting and interpolation machinery that std Rust uses. So the syntax your macro takes in and passes on is the same as you'd use for println!, format! and friends.

2 Likes

If you want key = value to be interpreted as a tracing field rather than as a string formatting argument, it has to go before the message string in the call to tracing::event!

I understand better now, thanks all!

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.