Using tracing logging crate to print out in json format

Having an issue with tracing printing out my implemented serializer for an enum. Basically, I implemented the enum to serialize to json without the tags. I've tested it, works fine. But when printing out the enum using the tracing crate it prints the tags.

I would assume there's something to configure on tracing side?

example

enum Primitive {
Bool(bool),
I32(i32),
I64(i64),
...
}

Still will print out Bool(true), I32(5), I64(6), etc ... in the tracing logs.

What do you mean by that exactly? If you implemented Display or Debug traits for your type to print out json, you can do something like info!(%your_variable) or info!(?your_variable) to use that implementation during logging. (I maintain a crate that conveniently wraps these kind of Display and Debug implementations in a derive macro.)

Alternatively you could use tracing_subscriber::fmt::SubscriberBuilder::json to print the whole log message as json:

tracing_subscriber::fmt().json().finish();

Thanks. I should have done the Debug trait, but it didn't cross my mind. I thought the Serialize json trait would be appropriate.

That was it, I had to implement the Debug trait to print out to json format.

By the way, the tracing crate is configured to print out json. I simply didn't want the tags to be logged.

1 Like