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
106 #[cfg(test)]
107 {
108 print!("otel_warn: name={}", $name);
109 $(
110 print!(", {}={}", stringify!($key), $value);
111 )+
112 print!("\n");
113 }
114
115 #[cfg(all(not(feature = "internal-logs"), not(test)))]
116 {
117 let _ = ($name, $($value),+); // Compiler will optimize this out as it's unused.
118 }
119 };
120}
121
122/// Macro for logging debug messages in OpenTelemetry.
123///
124/// # Fields:
125/// - `name`: The operation or action being logged.
126/// - Additional optional key-value pairs can be passed as attributes.
127///
128/// # Example:
129/// ```rust
130/// use opentelemetry::otel_debug;
131/// otel_debug!(name: "debug_operation", debug_level = "high", version = "1.0.0");
132/// ```
133#[macro_export]
134macro_rules! otel_debug {
135 (name: $name:expr $(,)?) => {
136 #[cfg(feature = "internal-logs")]
137 {
138 $crate::_private::debug!(name: $name, target: env!("CARGO_PKG_NAME"), name = $name, "");
139 }
140
141 #[cfg(test)]
142 {
143 print!("otel_debug: name={}\n", $name);
144 }
145
146 #[cfg(all(not(feature = "internal-logs"), not(test)))]
147 {
148 let _ = $name; // Compiler will optimize this out as it's unused.
149 }
150 };
151 (name: $name:expr, $($key:ident = $value:expr),+ $(,)?) => {
152 #[cfg(feature = "internal-logs")]
153 {
154 $crate::_private::debug!(name: $name, target: env!("CARGO_PKG_NAME"), name = $name, $($key = $value),+, "");
155 }
156
157 #[cfg(test)]
158 {
159 print!("otel_debug: name={}", $name);
160 $(
161 print!(", {}={}", stringify!($key), $value);
162 )+
163 print!("\n");
164 }
165
166 #[cfg(all(not(feature = "internal-logs"), not(test)))]
167 {
168 let _ = ($name, $($value),+); // Compiler will optimize this out as it's unused.
169 }
170 };
171}
172
173/// Macro for logging error messages in OpenTelemetry.
174///
175/// # Fields:
176/// - `name`: The operation or action being logged.
177/// - Additional optional key-value pairs can be passed as attributes.
178///
179/// # Example:
180/// ```rust
181/// use opentelemetry::otel_error;
182/// otel_error!(name: "export_failure", error_code = 500, version = "1.0.0");
183/// ```
184#[macro_export]
185macro_rules! otel_error {
186 (name: $name:expr $(,)?) => {
187 #[cfg(feature = "internal-logs")]
188 {
189 $crate::_private::error!(name: $name, target: env!("CARGO_PKG_NAME"), name = $name, "");
190 }
191
192 #[cfg(test)]
193 {
194 print!("otel_error: name={}\n", $name);
195 }
196
197 #[cfg(all(not(feature = "internal-logs"), not(test)))]
198 {
199 let _ = $name; // Compiler will optimize this out as it's unused.
200 }
201 };
202 (name: $name:expr, $($key:ident = $value:expr),+ $(,)?) => {
203 #[cfg(feature = "internal-logs")]
204 {
205 $crate::_private::error!(name: $name,
206 target: env!("CARGO_PKG_NAME"),
207 name = $name,
208 $($key = {
209 $value
210 }),+,
211 ""
212 )
213 }
214
215 #[cfg(test)]
216 {
217 print!("otel_error: name={}", $name);
218 $(
219 print!(", {}={}", stringify!($key), $value);
220 )+
221 print!("\n");
222 }
223
224 #[cfg(all(not(feature = "internal-logs"), not(test)))]
225 {
226 let _ = ($name, $($value),+); // Compiler will optimize this out as it's unused.
227 }
228 };
229}