Skip to content

Commit 4b19632

Browse files
wyfoJoseph Perezhawkw
authored andcommitted
tracing: allow constant field names in macros (#2617)
I've found myself in the case where I wanted to have customized event field name for different trait implementations. In fact, these implementations are completely unrelated (in separate applications), so, in this use case, I find more readable to have `foo="some_id"` and `bar=16` instead of `resource="foo" value="some_id"` and `resource=bar value=16` Because events only accept identifier or literal as field name, this is quite cumbersome/impossible to do. A simple solution could be to make events accept constant expression too; in my use case, I could then add a associated constant to my trait. This PR proposes a new syntax for using constant field names: ```rust tracing::debug!({ CONSTANT_EXPR } = "foo"); ``` This is the same syntax than constant expression, so it should be quite intuitive. To avoid constant expression names conflict, internal variables of macro expansion have been prefixed with `__`, e.g. `__CALLSITE`. Co-authored-by: Joseph Perez <[email protected]> Co-authored-by: Eliza Weisman <[email protected]>
1 parent 8cb05ba commit 4b19632

File tree

5 files changed

+209
-72
lines changed

5 files changed

+209
-72
lines changed

tracing-subscriber/src/fmt/fmt_layer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,15 +1559,15 @@ mod test {
15591559
env::remove_var(NO_COLOR);
15601560
}
15611561

1562-
let subscriber: Subscriber<()> = fmt::Subscriber::default();
1562+
let layer: Layer<()> = fmt::Layer::default();
15631563
assert_eq!(
1564-
subscriber.is_ansi, ansi,
1564+
layer.is_ansi, ansi,
15651565
"NO_COLOR={:?}; Subscriber::default().is_ansi should be {}",
15661566
var, ansi
15671567
);
15681568

15691569
// with_ansi should override any `NO_COLOR` value
1570-
let subscriber: Subscriber<()> = fmt::Subscriber::default().with_ansi(true);
1570+
let subscriber: Layer<()> = fmt::Layer::default().with_ansi(true);
15711571
assert!(
15721572
subscriber.is_ansi,
15731573
"NO_COLOR={:?}; Subscriber::default().with_ansi(true).is_ansi should be true",

tracing/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,19 @@
319319
//! # }
320320
//!```
321321
//!
322+
//! Constant expressions can also be used as field names. Constants
323+
//! must be enclosed in curly braces (`{}`) to indicate that the *value*
324+
//! of the constant is to be used as the field name, rather than the
325+
//! constant's name. For example:
326+
//! ```
327+
//! # use tracing::{span, Level};
328+
//! # fn main() {
329+
//! const RESOURCE_NAME: &str = "foo";
330+
//! // this span will have the field `foo = "some_id"`
331+
//! span!(Level::TRACE, "get", { RESOURCE_NAME } = "some_id");
332+
//! # }
333+
//!```
334+
//!
322335
//! The `?` sigil is shorthand that specifies a field should be recorded using
323336
//! its [`fmt::Debug`] implementation:
324337
//! ```

0 commit comments

Comments
 (0)