|
2 | 2 | #[path = "impl_.rs"] |
3 | 3 | pub mod __macros_impl; |
4 | 4 |
|
5 | | -/// Create a new span. |
| 5 | +/// Create a new [`Span`][tracing::Span]. This macro is a simplified version of `tracing::span!` which accepts |
| 6 | +/// a smaller set of possible arguments and syntaxes. |
6 | 7 | /// |
7 | | -/// The return type of this macro is a [`tracing::Span`], which can be used to enter the span |
8 | | -/// or to pass it to `tracing::Instrument` methods. |
| 8 | +/// In exchange, the macro automatically adds metadata which leads the span and its fields |
| 9 | +/// to present nicely in the Logfire UI. |
| 10 | +/// |
| 11 | +/// # Syntax |
| 12 | +/// |
| 13 | +/// The macro accepts the following syntax: |
| 14 | +/// |
| 15 | +/// ```rust,ignore |
| 16 | +/// span!( |
| 17 | +/// parent: tracing::Span, // optional, can emit this |
| 18 | +/// level: tracing::Level, // optional |
| 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 |
| 22 | +/// ) |
| 23 | +/// ``` |
| 24 | +/// |
| 25 | +/// The format string only accepts arguments by name. |
| 26 | +/// |
| 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. |
| 29 | +/// |
| 30 | +/// # Examples |
| 31 | +/// |
| 32 | +/// ```rust,no_run |
| 33 | +/// // Span without any arguments |
| 34 | +/// let root_span = logfire::span!("Root span"); |
| 35 | +/// |
| 36 | +/// // Span with attributes x and y |
| 37 | +/// let span = logfire::span!(parent: &root_span, "Child span", x = 42, y = "hello"); |
| 38 | +/// |
| 39 | +/// // Typically a span will be "entered" to set the parent implicitly |
| 40 | +/// root_span.in_scope(|| { |
| 41 | +/// // This span will be a child of root_span |
| 42 | +/// let child_span = logfire::span!("Nested span", x = 42, y = "hello"); |
| 43 | +/// |
| 44 | +/// // Debug-level child span |
| 45 | +/// let debug_span = logfire::span!(level: tracing::Level::DEBUG, "Debugging", x = 42, y = "hello"); |
| 46 | +/// }); |
| 47 | +/// |
| 48 | +/// // With x included in the formatted message but not as an attribute |
| 49 | +/// let x = 42; |
| 50 | +/// let span = logfire::span!("Span with x = {x}, y = {y}", y = "hello"); |
| 51 | +/// ``` |
9 | 52 | #[macro_export] |
10 | 53 | macro_rules! span { |
11 | 54 | (parent: $parent:expr, level: $level:expr, $format:expr $(, $arg:ident = $value:expr)* $(,)?) => { |
@@ -75,6 +118,58 @@ macro_rules! debug { |
75 | 118 | } |
76 | 119 |
|
77 | 120 | /// Export a log message at the specified level. |
| 121 | +/// |
| 122 | +/// # Syntax |
| 123 | +/// |
| 124 | +/// The macro accepts the following syntax: |
| 125 | +/// |
| 126 | +/// ```rust,ignore |
| 127 | +/// log!( |
| 128 | +/// parent: tracing::Span, // optional, can emit this |
| 129 | +/// tracing::Level, // required, see `info!` and variants for convenience |
| 130 | +/// "format string", // required, must be a format string accepted by `format!()` |
| 131 | +/// arg1 = value1, // optional, can be repeated |
| 132 | +/// .. // as many additional arg = value pairs as desired |
| 133 | +/// ) |
| 134 | +/// ``` |
| 135 | +/// |
| 136 | +/// The format string only accepts arguments by name. |
| 137 | +/// |
| 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. |
| 140 | +/// |
| 141 | +/// # Examples |
| 142 | +/// |
| 143 | +/// ```rust,no_run |
| 144 | +/// use tracing::Level; |
| 145 | +/// |
| 146 | +/// // Root span |
| 147 | +/// let root_span = logfire::span!("Root span"); |
| 148 | +/// |
| 149 | +/// // Log with attributes x and y |
| 150 | +/// logfire::log!(parent: &root_span, Level::INFO, "Child log", x = 42, y = "hello"); |
| 151 | +/// // or |
| 152 | +/// logfire::info!(parent: &root_span, "Child log", x = 42, y = "hello"); |
| 153 | +/// |
| 154 | +/// // Typically a span will be "entered" to set the parent implicitly |
| 155 | +/// root_span.in_scope(|| { |
| 156 | +/// // This logf will be a child of root_span |
| 157 | +/// logfire::log!(level::INFO, "Nested log", x = 42, y = "hello"); |
| 158 | +/// // or |
| 159 | +/// logfire::info!("Nested log", x = 42, y = "hello"); |
| 160 | +/// |
| 161 | +/// // Debug-level child log |
| 162 | +/// logfire::log!(Level::DEBUG, "Debugging", x = 42, y = "hello"); |
| 163 | +/// // or |
| 164 | +/// logfire::debug!("Debugging", x = 42, y = "hello"); |
| 165 | +/// }); |
| 166 | +/// |
| 167 | +/// // With x included in the formatted message but not as an attribute |
| 168 | +/// let x = 42; |
| 169 | +/// logfire::log!(Level::INFO, "Log with x = {x}, y = {y}", y = "hello"); |
| 170 | +/// // or |
| 171 | +/// logfire::info!("Log with x = {x}, y = {y}", y = "hello"); |
| 172 | +/// ``` |
78 | 173 | #[macro_export] |
79 | 174 | macro_rules! log { |
80 | 175 | (parent: $parent:expr, $level:expr, $format:expr, $($($arg:ident = $value:expr),+)?) => {{ |
|
0 commit comments