tracing-test icon indicating copy to clipboard operation
tracing-test copied to clipboard

Assert passed fields/variables in the logs

Open BenJeau opened this issue 4 years ago • 3 comments

I'm looking into asserting the recorded fields from tracing. It doesn't seem like this testing lib supports it (please correct me if I'm wrong).

Also, please let me know if you would be open to a PR adding this functionality, if you think it would be useful for others - I may dive into this lib and add that feature.

BenJeau avatar Sep 02 '21 04:09 BenJeau

Yes, you're right, I think this is not currently supported.

Right now the subscriber simply writes bytes to a Vec writer, and the default output doesn't contain recorded fields I think. I'm not 100% sure about how the recorded fields are implemented, but I assume that the subscriber is responsible for formatting these logs and fields, right?

So what we could do is encoding the logs into a serialized format, and then the assertion functions would decode the lines again into a LogCall or LogLine struct. Or maybe it could even be decoded into the type that tracing uses internally to store the logs.

That was just a quick brainstorming. A pull request - or even just a comment with a concrete proposal - would be welcome!

dbrgn avatar Nov 25 '21 11:11 dbrgn

I'm searching online and it seems like you can get more data with layers from tracing, like here https://burgers.io/custom-logging-in-rust-using-tracing - although it would require a bit of rewrite, at least for the subscriber.rs.

BenJeau avatar Feb 02 '22 21:02 BenJeau

The types already in tracing, the following seem interesting:

  • Event
    • fields
    • parent span (root, itself, or ID)
    • Metadata
      • name of span
      • target
      • module path
      • file name
      • line
      • fields
      • kind

BenJeau avatar Feb 02 '22 21:02 BenJeau