saluki_core/data_model/event/log/
mod.rs

1//! Logs.
2
3use std::collections::HashMap;
4
5use saluki_context::tags::SharedTagSet;
6use serde_json::Value as JsonValue;
7use stringtheory::MetaString;
8
9/// A log event.
10#[derive(Clone, Debug, PartialEq)]
11pub struct Log {
12    /// Log message body.
13    message: MetaString,
14    /// Log status/severity (e.g., "info", "warn", "error").
15    status: Option<LogStatus>,
16    /// Log source
17    source: Option<MetaString>,
18    /// Hostname associated with the log.
19    hostname: MetaString,
20    /// Service associated with the log.
21    service: MetaString,
22    /// Tags of the log.
23    tags: SharedTagSet,
24    /// Additional properties of the log.
25    additional_properties: HashMap<MetaString, JsonValue>,
26}
27
28/// Log status.
29#[derive(Clone, Copy, Debug, PartialEq)]
30pub enum LogStatus {
31    /// Trace status.
32    Trace,
33    /// Emergency status.
34    Emergency,
35    /// Alert status.
36    Alert,
37    /// Fatal status.
38    Fatal,
39    /// Error status.
40    Error,
41    /// Warning status.
42    Warning,
43    /// Notice status.
44    Notice,
45    /// Info status.
46    Info,
47    /// Debug status.
48    Debug,
49}
50
51impl LogStatus {
52    /// Returns the log status in string format
53    pub fn as_str(&self) -> &str {
54        match self {
55            LogStatus::Trace => "Trace",
56            LogStatus::Emergency => "Emergency",
57            LogStatus::Alert => "Alert",
58            LogStatus::Fatal => "Fatal",
59            LogStatus::Error => "Error",
60            LogStatus::Warning => "Warning",
61            LogStatus::Notice => "Notice",
62            LogStatus::Info => "Info",
63            LogStatus::Debug => "Debug",
64        }
65    }
66}
67
68impl Log {
69    /// Creates a new `Log` with the given message.
70    pub fn new(message: impl Into<MetaString>) -> Self {
71        Self {
72            message: message.into(),
73            status: None,
74            source: None,
75            hostname: MetaString::empty(),
76            service: MetaString::empty(),
77            tags: SharedTagSet::default(),
78            additional_properties: HashMap::new(),
79        }
80    }
81
82    /// Sets the log status.
83    pub fn with_status(mut self, status: impl Into<Option<LogStatus>>) -> Self {
84        self.status = status.into();
85        self
86    }
87
88    /// Sets the log source.
89    pub fn with_source(mut self, source: impl Into<Option<MetaString>>) -> Self {
90        self.source = source.into();
91        self
92    }
93
94    /// Sets the hostname.
95    pub fn with_hostname(mut self, hostname: impl Into<Option<MetaString>>) -> Self {
96        self.hostname = hostname.into().unwrap_or_else(MetaString::empty);
97        self
98    }
99
100    /// Sets the service name.
101    pub fn with_service(mut self, service: impl Into<Option<MetaString>>) -> Self {
102        self.service = service.into().unwrap_or_else(MetaString::empty);
103        self
104    }
105
106    /// Sets the tags string.
107    pub fn with_tags(mut self, tags: impl Into<Option<SharedTagSet>>) -> Self {
108        self.tags = tags.into().unwrap_or_else(SharedTagSet::default);
109        self
110    }
111
112    /// Sets the addtional properties map.
113    pub fn with_additional_properties(
114        mut self, additional_properties: impl Into<Option<HashMap<MetaString, JsonValue>>>,
115    ) -> Self {
116        self.additional_properties = additional_properties.into().unwrap_or_default();
117        self
118    }
119
120    /// Returns the message string slice.
121    pub fn message(&self) -> &str {
122        &self.message
123    }
124
125    /// Returns the log status, if set.
126    pub fn status(&self) -> Option<LogStatus> {
127        self.status
128    }
129
130    /// Returns the log source, if set.
131    pub fn source(&self) -> &Option<MetaString> {
132        &self.source
133    }
134
135    /// Returns the hostname, if set.
136    pub fn hostname(&self) -> &str {
137        &self.hostname
138    }
139
140    /// Returns the service name, if set.
141    pub fn service(&self) -> &str {
142        &self.service
143    }
144
145    /// Returns the tags, if set.
146    pub fn tags(&self) -> &SharedTagSet {
147        &self.tags
148    }
149
150    /// Returns the additional properties map.
151    pub fn additional_properties(&self) -> &HashMap<MetaString, JsonValue> {
152        &self.additional_properties
153    }
154}