@@ -61,22 +61,41 @@ pub mod __macros_impl;
61
61
/// // Attributes can either be a single name or a dotted.name
62
62
/// // `dotted.name` is not available in the format string.
63
63
/// let span = logfire::span!("Span with x = {x}, y = {y}", y = "hello", foo.bar = 42);
64
+ ///
65
+ /// // If just an identifier is used without `= value`, it will be captured as an attribute
66
+ /// let foo = "bar";
67
+ /// let span = logfire::span!("Span with foo = {foo}", foo);
68
+ ///
69
+ /// // This identifier shorthand also works for struct fields
70
+ /// #[derive(Debug)]
71
+ /// struct User {
72
+ /// email: &'static str,
73
+ /// name: &'static str,
74
+ /// }
75
+ ///
76
+ /// let user = User {
77
+
78
+ /// name: "John Doe",
79
+ /// };
80
+ ///
81
+ /// // NB the dotted name is not available in the format string
82
+ /// let span = logfire::span!("Span user info", user.email, user.name);
64
83
/// ```
65
84
///
66
85
/// [attributes]: https://opentelemetry.io/docs/concepts/signals/traces/#attributes
67
86
#[ macro_export]
68
87
macro_rules! span {
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) ,* )
88
+ ( parent: $parent: expr, level: $level: expr, $format: expr $( , $( $path: ident) .+ $ ( = $value: expr) ? ) * $( , ) ?) => {
89
+ $crate:: __macros_impl:: tracing_span!( parent: $parent, $level, $format, $( $( $path) .+ $ ( = $value) ? ) ,* )
71
90
} ;
72
- ( parent: $parent: expr, $format: expr $( , $( $path: ident) .+ = $value: expr) * $( , ) ?) => {
73
- $crate:: __macros_impl:: tracing_span!( parent: $parent, tracing:: Level :: INFO , $format, $( $( $path) .+ = $value) ,* )
91
+ ( parent: $parent: expr, $format: expr $( , $( $path: ident) .+ $ ( = $value: expr) ? ) * $( , ) ?) => {
92
+ $crate:: __macros_impl:: tracing_span!( parent: $parent, tracing:: Level :: INFO , $format, $( $( $path) .+ $ ( = $value) ? ) ,* )
74
93
} ;
75
- ( level: $level: expr, $format: expr $( , $( $path: ident) .+ = $value: expr) * $( , ) ?) => {
76
- $crate:: __macros_impl:: tracing_span!( $level, $format, $( $( $path) .+ = $value) ,* )
94
+ ( level: $level: expr, $format: expr $( , $( $path: ident) .+ $ ( = $value: expr) ? ) * $( , ) ?) => {
95
+ $crate:: __macros_impl:: tracing_span!( $level, $format, $( $( $path) .+ $ ( = $value) ? ) ,* )
77
96
} ;
78
- ( $format: expr $( , $( $path: ident) .+ = $value: expr) * $( , ) ?) => {
79
- $crate:: __macros_impl:: tracing_span!( tracing:: Level :: INFO , $format, $( $( $path) .+ = $value) ,* )
97
+ ( $format: expr $( , $( $path: ident) .+ $ ( = $value: expr) ? ) * $( , ) ?) => {
98
+ $crate:: __macros_impl:: tracing_span!( tracing:: Level :: INFO , $format, $( $( $path) .+ $ ( = $value) ? ) ,* )
80
99
} ;
81
100
}
82
101
@@ -85,11 +104,11 @@ macro_rules! span {
85
104
/// See the [`log!`][macro@crate::log] macro for more details.
86
105
#[ macro_export]
87
106
macro_rules! error {
88
- ( parent: $parent: expr, $format: expr $( , $( $path: ident) .+ = $value: expr) * $( , ) ?) => {
89
- $crate:: log!( parent: $parent, tracing:: Level :: ERROR , $format, $( $( $path) .+ = $value) ,* )
107
+ ( parent: $parent: expr, $format: expr $( , $( $path: ident) .+ $ ( = $value: expr) ? ) * $( , ) ?) => {
108
+ $crate:: log!( parent: $parent, tracing:: Level :: ERROR , $format, $( $( $path) .+ $ ( = $value) ? ) ,* )
90
109
} ;
91
- ( $format: expr $( , $( $path: ident) .+ = $value: expr) * $( , ) ?) => {
92
- $crate:: log!( tracing:: Level :: ERROR , $format, $( $( $path) .+ = $value) ,* )
110
+ ( $format: expr $( , $( $path: ident) .+ $ ( = $value: expr) ? ) * $( , ) ?) => {
111
+ $crate:: log!( tracing:: Level :: ERROR , $format, $( $( $path) .+ $ ( = $value) ? ) ,* )
93
112
} ;
94
113
}
95
114
@@ -98,11 +117,11 @@ macro_rules! error {
98
117
/// See the [`log!`][macro@crate::log] macro for more details.
99
118
#[ macro_export]
100
119
macro_rules! warn {
101
- ( parent: $parent: expr, $format: expr $( , $( $path: ident) .+ = $value: expr) * $( , ) ?) => {
102
- $crate:: log!( parent: $parent, tracing:: Level :: WARN , $format, $( $( $path) .+ = $value) ,* )
120
+ ( parent: $parent: expr, $format: expr $( , $( $path: ident) .+ $ ( = $value: expr) ? ) * $( , ) ?) => {
121
+ $crate:: log!( parent: $parent, tracing:: Level :: WARN , $format, $( $( $path) .+ $ ( = $value) ? ) ,* )
103
122
} ;
104
- ( $format: expr $( , $( $path: ident) .+ = $value: expr) * $( , ) ?) => {
105
- $crate:: log!( tracing:: Level :: WARN , $format, $( $( $path) .+ = $value) ,* )
123
+ ( $format: expr $( , $( $path: ident) .+ $ ( = $value: expr) ? ) * $( , ) ?) => {
124
+ $crate:: log!( tracing:: Level :: WARN , $format, $( $( $path) .+ $ ( = $value) ? ) ,* )
106
125
} ;
107
126
}
108
127
@@ -111,11 +130,11 @@ macro_rules! warn {
111
130
/// See the [`log!`][macro@crate::log] macro for more details.
112
131
#[ macro_export]
113
132
macro_rules! info {
114
- ( parent: $parent: expr, $format: expr $( , $( $path: ident) .+ = $value: expr) * $( , ) ?) => {
115
- $crate:: log!( parent: $parent, tracing:: Level :: INFO , $format, $( $( $path) .+ = $value) ,* )
133
+ ( parent: $parent: expr, $format: expr $( , $( $path: ident) .+ $ ( = $value: expr) ? ) * $( , ) ?) => {
134
+ $crate:: log!( parent: $parent, tracing:: Level :: INFO , $format, $( $( $path) .+ $ ( = $value) ? ) ,* )
116
135
} ;
117
- ( $format: expr $( , $( $path: ident) .+ = $value: expr) * $( , ) ?) => {
118
- $crate:: log!( tracing:: Level :: INFO , $format, $( $( $path) .+ = $value) ,* )
136
+ ( $format: expr $( , $( $path: ident) .+ $ ( = $value: expr) ? ) * $( , ) ?) => {
137
+ $crate:: log!( tracing:: Level :: INFO , $format, $( $( $path) .+ $ ( = $value) ? ) ,* )
119
138
} ;
120
139
}
121
140
@@ -124,11 +143,11 @@ macro_rules! info {
124
143
/// See the [`log!`][macro@crate::log] macro for more details.
125
144
#[ macro_export]
126
145
macro_rules! debug {
127
- ( parent: $parent: expr, $format: expr $( , $( $path: ident) .+ = $value: expr) * $( , ) ?) => {
128
- $crate:: log!( parent: $parent, tracing:: Level :: DEBUG , $format, $( $( $path) .+ = $value) ,* )
146
+ ( parent: $parent: expr, $format: expr $( , $( $path: ident) .+ $ ( = $value: expr) ? ) * $( , ) ?) => {
147
+ $crate:: log!( parent: $parent, tracing:: Level :: DEBUG , $format, $( $( $path) .+ $ ( = $value) ? ) ,* )
129
148
} ;
130
- ( $format: expr $( , $( $path: ident) .+ = $value: expr) * $( , ) ?) => {
131
- $crate:: log!( tracing:: Level :: DEBUG , $format, $( $( $path) .+ = $value) ,* )
149
+ ( $format: expr $( , $( $path: ident) .+ $ ( = $value: expr) ? ) * $( , ) ?) => {
150
+ $crate:: log!( tracing:: Level :: DEBUG , $format, $( $( $path) .+ $ ( = $value) ? ) ,* )
132
151
} ;
133
152
}
134
153
@@ -199,15 +218,38 @@ macro_rules! debug {
199
218
/// logfire::log!(Level::INFO, "Log with x = {x}, y = {y}", y = "hello", foo.bar = 42);
200
219
/// // or
201
220
/// logfire::info!("Log with x = {x}, y = {y}", y = "hello", foo.bar = 42);
221
+ ///
222
+ /// // If just an identifier is used without `= value`, it will be captured as an attribute
223
+ /// let foo = "bar";
224
+ /// logfire::log!(Level::INFO, "Log with foo = {foo}", foo);
225
+ /// // or
226
+ /// logfire::info!("Log with foo = {foo}", foo);
227
+ ///
228
+ /// // This identifier shorthand also works for struct fields
229
+ /// #[derive(Debug)]
230
+ /// struct User {
231
+ /// email: &'static str,
232
+ /// name: &'static str,
233
+ /// }
234
+ ///
235
+ /// let user = User {
236
+
237
+ /// name: "John Doe",
238
+ /// };
239
+ ///
240
+ /// // NB the dotted name is not available in the format string
241
+ /// logfire::log!(Level::INFO, "Log user info", user.email, user.name);
242
+ /// // or
243
+ /// logfire::info!("Log user info", user.email, user.name);
202
244
/// ```
203
245
///
204
246
/// [attributes]: https://opentelemetry.io/docs/concepts/signals/traces/#attributes
205
247
#[ macro_export]
206
248
macro_rules! log {
207
- ( parent: $parent: expr, $level: expr, $format: expr $( , $( $path: ident) .+ = $value: expr) * $( , ) ?) => {
208
- $crate:: __macros_impl:: log!( parent: $parent, $level, $format, $( $( $path) .+ = $value) ,* )
249
+ ( parent: $parent: expr, $level: expr, $format: expr $( , $( $path: ident) .+ $ ( = $value: expr) ? ) * $( , ) ?) => {
250
+ $crate:: __macros_impl:: log!( parent: $parent, $level, $format, $( $( $path) .+ $ ( = $value) ? ) ,* )
209
251
} ;
210
- ( $level: expr, $format: expr $( , $( $path: ident) .+ = $value: expr) * $( , ) ?) => {
211
- $crate:: __macros_impl:: log!( parent: & tracing:: Span :: current( ) , $level, $format, $( $( $path) .+ = $value) ,* )
252
+ ( $level: expr, $format: expr $( , $( $path: ident) .+ $ ( = $value: expr) ? ) * $( , ) ?) => {
253
+ $crate:: __macros_impl:: log!( parent: & tracing:: Span :: current( ) , $level, $format, $( $( $path) .+ $ ( = $value) ? ) ,* )
212
254
} ;
213
255
}
0 commit comments