veecle_telemetry/log.rs
1#[cfg(feature = "enable")]
2use crate::collector::get_collector;
3use crate::protocol::transient;
4
5/// Logs a message with the specified severity level and attributes.
6///
7/// Prefer using the macros.
8///
9/// This function creates a log message with the given severity, body text, and
10/// key-value attributes.
11/// If there is an active span context, the log message
12/// will automatically be correlated with the trace and span IDs.
13///
14/// # Arguments
15///
16/// * `severity` - The severity level of the log message
17/// * `body` - The main message text
18/// * `attributes` - Key-value pairs providing additional context
19///
20/// # Examples
21///
22/// ```rust
23/// use veecle_telemetry::span;
24/// use veecle_telemetry::log::log;
25/// use veecle_telemetry::protocol::transient::{Severity, KeyValue};
26///
27/// // Simple log message
28/// log(Severity::Info, "Server started", &[]);
29///
30/// // Log with attributes
31/// log(Severity::Warn, "High memory usage", &[
32/// KeyValue::new("memory_usage_percent", 85),
33/// KeyValue::new("available_mb", 512),
34/// ]);
35///
36/// // Log within a span context
37/// let span = span!("request_handler");
38/// let _guard = span.entered();
39/// log(Severity::Error, "Request failed", &[KeyValue::new("error_code", 500)]);
40/// ```
41///
42/// # Conditional Compilation
43///
44/// When the `enable` feature is disabled, this function compiles to a no-op
45/// and has zero runtime overhead.
46pub fn log<'a>(
47 severity: transient::Severity,
48 body: &'a str,
49 attributes: &'a [transient::KeyValue<'a>],
50) {
51 #[cfg(not(feature = "enable"))]
52 {
53 let _ = (severity, body, attributes);
54 }
55
56 #[cfg(feature = "enable")]
57 {
58 get_collector().log_message(severity, body, attributes);
59 }
60}