saluki_core/data_model/event/log/
mod.rs

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