opentelemetry/global/internal_logging.rs
1#![allow(unused_macros)]
2///
3/// **Note**: These macros (`otel_info!`, `otel_warn!`, `otel_debug!`, and `otel_error!`) are intended to be used
4/// **internally within OpenTelemetry code** or for **custom exporters, processors and other plugins**. They are not designed
5/// for general application logging and should not be used for that purpose.
6///
7/// When running tests with `--nocapture`, these macros will print their output to stdout. This is useful for debugging
8/// test failures and understanding the flow of operations during testing.
9///
10/// Macro for logging informational messages in OpenTelemetry.
11///
12/// # Fields:
13/// - `name`: The operation or action being logged.
14/// - Additional optional key-value pairs can be passed as attributes.
15///
16/// # Example:
17/// ```rust
18/// use opentelemetry::otel_info;
19/// otel_info!(name: "sdk_start", version = "1.0.0", schema_url = "http://example.com");
20/// ```
21///
22// TODO: Remove `name` attribute duplication in logging macros below once `tracing::Fmt` supports displaying `name`.
23// See issue: https://github.com/tokio-rs/tracing/issues/2774
24#[macro_export]
25macro_rules! otel_info {
26 (name: $name:expr $(,)?) => {
27 #[cfg(feature = "internal-logs")]
28 {
29 $crate::_private::info!( name: $name, target: env!("CARGO_PKG_NAME"), name = $name);
30 }
31
32 #[cfg(test)]
33 {
34 print!("otel_info: name={}\n", $name);
35 }
36
37 #[cfg(all(not(feature = "internal-logs"), not(test)))]
38 {
39 let _ = $name; // Compiler will optimize this out as it's unused.
40 }
41 };
42 (name: $name:expr, $($key:ident = $value:expr),+ $(,)?) => {
43 #[cfg(feature = "internal-logs")]
44 {
45 $crate::_private::info!(name: $name, target: env!("CARGO_PKG_NAME"), name = $name, $($key = $value),+);
46 }
47
48 #[cfg(test)]
49 {
50 print!("otel_info: name={}", $name);
51 $(
52 print!(", {}={}", stringify!($key), $value);
53 )+
54 print!("\n");
55 }
56
57 #[cfg(all(not(feature = "internal-logs"), not(test)))]
58 {
59 let _ = ($name, $($value),+); // Compiler will optimize this out as it's unused.
60 }
61 };
62}
63
64/// Macro for logging warning messages in OpenTelemetry.
65///
66/// # Fields:
67/// - `name`: The operation or action being logged.
68/// - Additional optional key-value pairs can be passed as attributes.
69///
70/// # Example:
71/// ```rust
72/// use opentelemetry::otel_warn;
73/// otel_warn!(name: "export_warning", error_code = 404, version = "1.0.0");
74/// ```
75#[macro_export]
76macro_rules! otel_warn {
77 (name: $name:expr $(,)?) => {
78 #[cfg(feature = "internal-logs")]
79 {
80 $crate::_private::warn!(name: $name, target: env!("CARGO_PKG_NAME"), name = $name);
81 }
82
83 #[cfg(test)]
84 {
85 print!("otel_warn: name={}\n", $name);
86 }
87
88 #[cfg(all(not(feature = "internal-logs"), not(test)))]
89 {
90 let _ = $name; // Compiler will optimize this out as it's unused.
91 }
92 };
93 (name: $name:expr, $($key:ident = $value:expr),+ $(,)?) => {
94 #[cfg(feature = "internal-logs")]
95 {
96 $crate::_private::warn!(name: $name,
97 target: env!("CARGO_PKG_NAME"),
98 name = $name,
99 $($key = {
100 $value
101 }),+,
102 )
103 }
104
105 #[cfg(test)]
106 {
107 print!("otel_warn: name={}", $name);
108 $(
109 print!(", {}={}", stringify!($key), $value);
110 )+
111 print!("\n");
112 }
113
114 #[cfg(all(not(feature = "internal-logs"), not(test)))]
115 {
116 let _ = ($name, $($value),+); // Compiler will optimize this out as it's unused.
117 }
118 };
119}
120
121/// Macro for logging debug messages in OpenTelemetry.
122///
123/// # Fields:
124/// - `name`: The operation or action being logged.
125/// - Additional optional key-value pairs can be passed as attributes.
126///
127/// # Example:
128/// ```rust
129/// use opentelemetry::otel_debug;
130/// otel_debug!(name: "debug_operation", debug_level = "high", version = "1.0.0");
131/// ```
132#[macro_export]
133macro_rules! otel_debug {
134 (name: $name:expr $(,)?) => {
135 #[cfg(feature = "internal-logs")]
136 {
137 $crate::_private::debug!(name: $name, target: env!("CARGO_PKG_NAME"), name = $name);
138 }
139
140 #[cfg(test)]
141 {
142 print!("otel_debug: name={}\n", $name);
143 }
144
145 #[cfg(all(not(feature = "internal-logs"), not(test)))]
146 {
147 let _ = $name; // Compiler will optimize this out as it's unused.
148 }
149 };
150 (name: $name:expr, $($key:ident = $value:expr),+ $(,)?) => {
151 #[cfg(feature = "internal-logs")]
152 {
153 $crate::_private::debug!(name: $name, target: env!("CARGO_PKG_NAME"), name = $name, $($key = $value),+);
154 }
155
156 #[cfg(test)]
157 {
158 print!("otel_debug: name={}", $name);
159 $(
160 print!(", {}={}", stringify!($key), $value);
161 )+
162 print!("\n");
163 }
164
165 #[cfg(all(not(feature = "internal-logs"), not(test)))]
166 {
167 let _ = ($name, $($value),+); // Compiler will optimize this out as it's unused.
168 }
169 };
170}
171
172/// Macro for logging error messages in OpenTelemetry.
173///
174/// # Fields:
175/// - `name`: The operation or action being logged.
176/// - Additional optional key-value pairs can be passed as attributes.
177///
178/// # Example:
179/// ```rust
180/// use opentelemetry::otel_error;
181/// otel_error!(name: "export_failure", error_code = 500, version = "1.0.0");
182/// ```
183#[macro_export]
184macro_rules! otel_error {
185 (name: $name:expr $(,)?) => {
186 #[cfg(feature = "internal-logs")]
187 {
188 $crate::_private::error!(name: $name, target: env!("CARGO_PKG_NAME"), name = $name);
189 }
190
191 #[cfg(test)]
192 {
193 print!("otel_error: name={}\n", $name);
194 }
195
196 #[cfg(all(not(feature = "internal-logs"), not(test)))]
197 {
198 let _ = $name; // Compiler will optimize this out as it's unused.
199 }
200 };
201 (name: $name:expr, $($key:ident = $value:expr),+ $(,)?) => {
202 #[cfg(feature = "internal-logs")]
203 {
204 $crate::_private::error!(name: $name,
205 target: env!("CARGO_PKG_NAME"),
206 name = $name,
207 $($key = {
208 $value
209 }),+,
210 )
211 }
212
213 #[cfg(test)]
214 {
215 print!("otel_error: name={}", $name);
216 $(
217 print!(", {}={}", stringify!($key), $value);
218 )+
219 print!("\n");
220 }
221
222 #[cfg(all(not(feature = "internal-logs"), not(test)))]
223 {
224 let _ = ($name, $($value),+); // Compiler will optimize this out as it's unused.
225 }
226 };
227}