Is there a way to set custom field globally for tracing_subscriber without much effort?

1.1k views Asked by At

I use tracing and tracing_subscriber Rust libraries to write logs in the app and I would like to pass custom field globally for all log messages to separate different instances of the app in a log db. For example, I would like to put instance id in all log messages of that instance. As such,

{"timestamp": "...", "level": "INFO", "name": "instance1", "msg": "..."}
{"timestamp": "...", "level": "ERROR", "name": "instance1", "msg": "..."}
{"timestamp": "...", "level": "INFO", "name": "instance1", "msg": "..."}

Is there a way to implement this with standard tracing_subscriber and not implementing my own Layer?

1

There are 1 answers

0
Netwave On BEST ANSWER

You can use spans to instrument your code and add metadata as needed for all subsecuent logging:

span!(Level::INFO, "my_span", name= "instance1");