control level of synthetic events from .with_span_events()?

118 Views Asked by At

I'm able to emit events using https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/struct.Layer.html#method.with_span_events, but they are created at TRACE level. Is there any way to cause these to be created at a different level?

3

There are 3 best solutions below

1
Michael On

You can create events using the event! macro with the desired level specified, e.g.

event!(Level::ERROR, %error, "Received error");

Or even better you can use the shorthand macros for each level

error!(%error, "Received error");
0
so61pi On

From https://docs.rs/tracing/latest/tracing/attr.instrument.html:

Unless overridden, a span with the INFO level will be generated...

So you can change the generated span events' level by setting level argument of #[instrument] macro.

#[instrument(level = "debug")]
pub fn my_function() {
}
0
Chayim Friedman On

The level is determined by the span's itself level, and this is hardcoded and cannot be changed.

The level of the span can be set:

  • If you are using the span!() macro, the first argument is the level. Or you can use one of the shortcut macros: trace_span!() etc..

  • If you are using #[instrument], you can pass a level = "<level>" argument to it.