@@ -17,15 +17,24 @@ pub mod __macros_impl;
17
17
/// parent: tracing::Span, // optional, can emit this
18
18
/// level: tracing::Level, // optional
19
19
/// "format string", // required, must be a format string accepted by `format!()`
20
- /// arg1 = value1 , // optional, can be repeated
21
- /// .. // as many additional arg = value pairs as desired
20
+ /// attr = value , // optional attributes , can be repeated
21
+ /// .. // as many additional attr = value pairs as desired
22
22
/// )
23
23
/// ```
24
24
///
25
- /// The format string only accepts arguments by name.
25
+ /// ## Attributes
26
+ ///
27
+ /// The `attr = value` pairs are captured as [attributes] on the span.
28
+ ///
29
+ /// `dotted.name = value` is also supported (and encouraged by Opentelemetry) to namespace
30
+ /// attributes. However, dotted names are not supported in Rust format strings.
31
+ //
32
+ /// ## Formatting
26
33
///
27
- /// These can be automatically captured from the surrounding scope by the macro,
28
- /// in which case they will render in the message but will not be.
34
+ /// The format string only accepts arguments by name. These can either be explicitly passed
35
+ /// to `span!` as an attribute or captured from the surrounding scope by the macro. If captured
36
+ /// from the surrounding scope, they will only be used as a format string and not exported
37
+ /// as attributes.
29
38
///
30
39
/// # Examples
31
40
///
@@ -48,20 +57,26 @@ pub mod __macros_impl;
48
57
/// // With x included in the formatted message but not as an attribute
49
58
/// let x = 42;
50
59
/// let span = logfire::span!("Span with x = {x}, y = {y}", y = "hello");
60
+ ///
61
+ /// // Attributes can either be a single name or a dotted.name
62
+ /// // `dotted.name` is not available in the format string.
63
+ /// let span = logfire::span!("Span with x = {x}, y = {y}", y = "hello", foo.bar = 42);
51
64
/// ```
65
+ ///
66
+ /// [attributes]: https://opentelemetry.io/docs/concepts/signals/traces/#attributes
52
67
#[ macro_export]
53
68
macro_rules! span {
54
- ( parent: $parent: expr, level: $level: expr, $format: expr $( , $arg : ident = $value: expr) * $( , ) ?) => {
55
- $crate:: __macros_impl:: tracing_span!( parent: $parent, $level, $format, $( $arg = $value) ,* )
69
+ ( parent: $parent: expr, level: $level: expr, $format: expr $( , $( $path : ident) .+ = $value: expr) * $( , ) ?) => {
70
+ $crate:: __macros_impl:: tracing_span!( parent: $parent, $level, $format, $( $( $path ) .+ = $value) ,* )
56
71
} ;
57
- ( parent: $parent: expr, $format: expr $( , $arg : ident = $value: expr) * $( , ) ?) => {
58
- $crate:: __macros_impl:: tracing_span!( parent: $parent, tracing:: Level :: INFO , $format, $( $arg = $value) ,* )
72
+ ( parent: $parent: expr, $format: expr $( , $( $path : ident) .+ = $value: expr) * $( , ) ?) => {
73
+ $crate:: __macros_impl:: tracing_span!( parent: $parent, tracing:: Level :: INFO , $format, $( $( $path ) .+ = $value) ,* )
59
74
} ;
60
- ( level: $level: expr, $format: expr $( , $arg : ident = $value: expr) * $( , ) ?) => {
61
- $crate:: __macros_impl:: tracing_span!( $level, $format, $( $arg = $value) ,* )
75
+ ( level: $level: expr, $format: expr $( , $( $path : ident) .+ = $value: expr) * $( , ) ?) => {
76
+ $crate:: __macros_impl:: tracing_span!( $level, $format, $( $( $path ) .+ = $value) ,* )
62
77
} ;
63
- ( $format: expr $( , $arg : ident = $value: expr) * $( , ) ?) => {
64
- $crate:: __macros_impl:: tracing_span!( tracing:: Level :: INFO , $format, $( $arg = $value) ,* )
78
+ ( $format: expr $( , $( $path : ident) .+ = $value: expr) * $( , ) ?) => {
79
+ $crate:: __macros_impl:: tracing_span!( tracing:: Level :: INFO , $format, $( $( $path ) .+ = $value) ,* )
65
80
} ;
66
81
}
67
82
@@ -70,11 +85,11 @@ macro_rules! span {
70
85
/// See the [`log!`][macro@crate::log] macro for more details.
71
86
#[ macro_export]
72
87
macro_rules! error {
73
- ( parent: $parent: expr, $format: expr $( , $arg : ident = $value: expr) * $( , ) ?) => {
74
- $crate:: log!( parent: $parent, tracing:: Level :: ERROR , $format, $( $arg = $value) ,* )
88
+ ( parent: $parent: expr, $format: expr $( , $( $path : ident) .+ = $value: expr) * $( , ) ?) => {
89
+ $crate:: log!( parent: $parent, tracing:: Level :: ERROR , $format, $( $( $path ) .+ = $value) ,* )
75
90
} ;
76
- ( $format: expr $( , $arg : ident = $value: expr) * $( , ) ?) => {
77
- $crate:: log!( tracing:: Level :: ERROR , $format, $( $arg = $value) ,* )
91
+ ( $format: expr $( , $( $path : ident) .+ = $value: expr) * $( , ) ?) => {
92
+ $crate:: log!( tracing:: Level :: ERROR , $format, $( $( $path ) .+ = $value) ,* )
78
93
} ;
79
94
}
80
95
@@ -83,11 +98,11 @@ macro_rules! error {
83
98
/// See the [`log!`][macro@crate::log] macro for more details.
84
99
#[ macro_export]
85
100
macro_rules! warn {
86
- ( parent: $parent: expr, $format: expr $( , $arg : ident = $value: expr) * $( , ) ?) => {
87
- $crate:: log!( parent: $parent, tracing:: Level :: WARN , $format, $( $arg = $value) ,* )
101
+ ( parent: $parent: expr, $format: expr $( , $( $path : ident) .+ = $value: expr) * $( , ) ?) => {
102
+ $crate:: log!( parent: $parent, tracing:: Level :: WARN , $format, $( $( $path ) .+ = $value) ,* )
88
103
} ;
89
- ( $format: expr $( , $arg : ident = $value: expr) * $( , ) ?) => {
90
- $crate:: log!( tracing:: Level :: WARN , $format, $( $arg = $value) ,* )
104
+ ( $format: expr $( , $( $path : ident) .+ = $value: expr) * $( , ) ?) => {
105
+ $crate:: log!( tracing:: Level :: WARN , $format, $( $( $path ) .+ = $value) ,* )
91
106
} ;
92
107
}
93
108
@@ -96,24 +111,24 @@ macro_rules! warn {
96
111
/// See the [`log!`][macro@crate::log] macro for more details.
97
112
#[ macro_export]
98
113
macro_rules! info {
99
- ( parent: $parent: expr, $format: expr $( , $arg : ident = $value: expr) * $( , ) ?) => {
100
- $crate:: log!( parent: $parent, tracing:: Level :: INFO , $format, $( $arg = $value) ,* )
114
+ ( parent: $parent: expr, $format: expr $( , $ ( $path : ident) .+ = $value: expr) * $( , ) ?) => {
115
+ $crate:: log!( parent: $parent, tracing:: Level :: INFO , $format, $( $( $path ) .+ = $value) ,* )
101
116
} ;
102
- ( $format: expr $( , $arg : ident = $value: expr) * $( , ) ?) => {
103
- $crate:: log!( tracing:: Level :: INFO , $format, $( $arg = $value) ,* )
117
+ ( $format: expr $( , $( $path : ident) .+ = $value: expr) * $( , ) ?) => {
118
+ $crate:: log!( tracing:: Level :: INFO , $format, $( $( $path ) .+ = $value) ,* )
104
119
} ;
105
120
}
106
121
107
- /// Emit a log at the ERROR level.
122
+ /// Emit a log at the DEBUG level.
108
123
///
109
124
/// See the [`log!`][macro@crate::log] macro for more details.
110
125
#[ macro_export]
111
126
macro_rules! debug {
112
- ( parent: $parent: expr, $format: expr $( , $arg : ident = $value: expr) * $( , ) ?) => {
113
- $crate:: log!( parent: $parent, tracing:: Level :: DEBUG , $format, $( $arg = $value) ,* )
127
+ ( parent: $parent: expr, $format: expr $( , $ ( $path : ident) .+ = $value: expr) * $( , ) ?) => {
128
+ $crate:: log!( parent: $parent, tracing:: Level :: DEBUG , $format, $( $( $path ) .+ = $value) ,* )
114
129
} ;
115
- ( $format: expr $( , $arg : ident = $value: expr) * $( , ) ?) => {
116
- $crate:: log!( tracing:: Level :: DEBUG , $format, $( $arg = $value) ,* )
130
+ ( $format: expr $( , $( $path : ident) .+ = $value: expr) * $( , ) ?) => {
131
+ $crate:: log!( tracing:: Level :: DEBUG , $format, $( $( $path ) .+ = $value) ,* )
117
132
} ;
118
133
}
119
134
@@ -128,15 +143,24 @@ macro_rules! debug {
128
143
/// parent: tracing::Span, // optional, can emit this
129
144
/// tracing::Level, // required, see `info!` and variants for convenience
130
145
/// "format string", // required, must be a format string accepted by `format!()`
131
- /// arg1 = value1 , // optional, can be repeated
146
+ /// attr = value , // optional attributes , can be repeated
132
147
/// .. // as many additional arg = value pairs as desired
133
148
/// )
134
149
/// ```
135
150
///
136
- /// The format string only accepts arguments by name.
151
+ /// ## Attributes
137
152
///
138
- /// These can be automatically captured from the surrounding scope by the macro,
139
- /// in which case they will render in the message but will not be.
153
+ /// The `attr = value` pairs are captured as [attributes] on the span.
154
+ ///
155
+ /// `dotted.name = value` is also supported (and encouraged by Opentelemetry) to namespace
156
+ /// attributes. However, dotted names are not supported in Rust format strings.
157
+ //
158
+ /// ## Formatting
159
+ ///
160
+ /// The format string only accepts arguments by name. These can either be explicitly passed
161
+ /// to `span!` as an attribute or captured from the surrounding scope by the macro. If captured
162
+ /// from the surrounding scope, they will only be used as a format string and not exported
163
+ /// as attributes.
140
164
///
141
165
/// # Examples
142
166
///
@@ -169,45 +193,19 @@ macro_rules! debug {
169
193
/// logfire::log!(Level::INFO, "Log with x = {x}, y = {y}", y = "hello");
170
194
/// // or
171
195
/// logfire::info!("Log with x = {x}, y = {y}", y = "hello");
196
+ ///
197
+ /// // Attributes can either be a single name or a dotted.name
198
+ /// // `dotted.name` is not available in the format string.
199
+ /// logfire::log!(Level::INFO, "Log with x = {x}, y = {y}", y = "hello", foo.bar = 42);
200
+ /// // or
201
+ /// logfire::info!("Log with x = {x}, y = {y}", y = "hello", foo.bar = 42);
172
202
/// ```
173
203
#[ macro_export]
174
204
macro_rules! log {
175
- ( parent: $parent: expr, $level: expr, $format: expr, $( $( $arg: ident = $value: expr) ,+) ?) => { {
176
- if tracing:: span_enabled!( $level) {
177
- // bind args early to avoid multiple evaluation
178
- $( $( let $arg = $value; ) * ) ?
179
- $crate:: __macros_impl:: export_log_span(
180
- $format,
181
- $parent,
182
- format!( $format) ,
183
- $level,
184
- $crate:: __json_schema!( $( $( $arg) ,+) ?) ,
185
- file!( ) ,
186
- line!( ) ,
187
- module_path!( ) ,
188
- [
189
- $( $( $crate:: __macros_impl:: LogfireValue :: new( stringify!( $arg) , $crate:: __macros_impl:: converter( & $arg) . convert_value( $arg) ) , ) * ) ?
190
- ]
191
- ) ;
192
- }
193
- } } ;
194
- ( $level: expr, $format: expr, $( $( $arg: ident = $value: expr) ,+) ?) => { {
195
- if tracing:: span_enabled!( $level) {
196
- // bind args early to avoid multiple evaluation
197
- $( $( let $arg = $value; ) * ) ?
198
- $crate:: __macros_impl:: export_log_span(
199
- $format,
200
- & tracing:: Span :: current( ) ,
201
- format!( $format) ,
202
- $level,
203
- $crate:: __json_schema!( $( $( $arg) ,+) ?) ,
204
- file!( ) ,
205
- line!( ) ,
206
- module_path!( ) ,
207
- [
208
- $( $( $crate:: __macros_impl:: LogfireValue :: new( stringify!( $arg) , $crate:: __macros_impl:: converter( & $arg) . convert_value( $arg) ) , ) * ) ?
209
- ]
210
- ) ;
211
- }
212
- } } ;
205
+ ( parent: $parent: expr, $level: expr, $format: expr $( , $( $path: ident) .+ = $value: expr) * $( , ) ?) => {
206
+ $crate:: __macros_impl:: log!( parent: $parent, $level, $format, $( $( $path) .+ = $value) ,* )
207
+ } ;
208
+ ( $level: expr, $format: expr $( , $( $path: ident) .+ = $value: expr) * $( , ) ?) => {
209
+ $crate:: __macros_impl:: log!( parent: & tracing:: Span :: current( ) , $level, $format, $( $( $path) .+ = $value) ,* )
210
+ } ;
213
211
}
0 commit comments