1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use std::{
collections::BTreeMap,
time::{Duration, SystemTime, UNIX_EPOCH},
};
use crate::{
logging::{
log_level::LogLevel,
log_message::{LogMessage, MessageId},
logger::initialize_terminal_logger,
},
newtypes::CorrelationId,
utils::jsonify,
};
pub mod log_level;
pub mod log_message;
pub mod log_settings;
#[macro_use]
pub mod logger;
#[cfg(test)]
mod tests;
pub const GAUGE: &str = "gauge";
#[inline]
pub fn log(log_level: LogLevel, log_message: &str) -> Option<MessageId> {
initialize_terminal_logger();
let log_settings_provider = log_settings::get_log_settings_provider();
if log_settings_provider.filter(log_level) {
return None;
}
let log_message = LogMessage::new_msg(log_settings_provider, log_level, log_message.to_owned());
let json = jsonify(&log_message, false);
log::log!(
log_level.into(),
"{timestamp} {loglevel} {priority} {hostname} {facility} payload={payload}",
timestamp = log_message.timestamp,
loglevel = log_message.log_level.to_uppercase(),
priority = log_message.priority.value(),
hostname = log_message.host_name.value(),
facility = log_message.process_name.value(),
payload = json
);
Some(log_message.message_id)
}
#[inline]
pub fn log_details(
log_level: LogLevel,
message_format: String,
properties: BTreeMap<String, String>,
) -> Option<MessageId> {
initialize_terminal_logger();
let log_settings_provider = log_settings::get_log_settings_provider();
if log_settings_provider.filter(log_level) {
return None;
}
let log_message =
LogMessage::new_props(log_settings_provider, log_level, message_format, properties);
let json = jsonify(&log_message, false);
log::log!(
log_level.into(),
"{timestamp} {loglevel} {priority} {hostname} {facility} payload={payload}",
timestamp = log_message.timestamp,
loglevel = log_message.log_level.to_uppercase(),
priority = log_message.priority.value(),
hostname = log_message.host_name.value(),
facility = log_message.process_name.value(),
payload = json
);
Some(log_message.message_id)
}
#[inline]
pub fn log_duration(
correlation_id: CorrelationId,
metric: &str,
tag: &str,
duration: Duration,
) -> Option<MessageId> {
initialize_terminal_logger();
let duration_in_seconds: f64 = duration.as_secs_f64();
log_metric(
correlation_id,
metric,
tag,
"duration_in_seconds",
duration_in_seconds,
)
}
#[inline]
pub fn log_metric(
correlation_id: CorrelationId,
metric: &str,
tag: &str,
metric_key: &str,
metric_value: f64,
) -> Option<MessageId> {
initialize_terminal_logger();
let log_settings_provider = log_settings::get_log_settings_provider();
const METRIC_LOG_LEVEL: LogLevel = LogLevel::Metric;
if log_settings_provider.filter(METRIC_LOG_LEVEL) {
return None;
}
let mut properties: BTreeMap<String, String> = BTreeMap::new();
let from_epoch = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("UNIX EPOCH ERROR");
let milliseconds_since_epoch = from_epoch.as_millis() as i64;
let tsd_metric = format!(
"{}{{tag=\"{}\"}} {} {:?}",
metric, tag, metric_value, milliseconds_since_epoch
);
properties.insert("correlation_id".to_string(), correlation_id.to_string());
properties.insert("time-series-data".to_string(), tsd_metric);
properties.insert(metric_key.to_string(), format!("{:?}", metric_value));
properties.insert(
"message".to_string(),
format!("{} {} {}", metric, tag, metric_value),
);
let message_format = String::from("{message}");
log_details(METRIC_LOG_LEVEL, message_format, properties)
}
#[inline]
pub fn log_fatal(log_message: &str) -> Option<MessageId> {
log(LogLevel::Fatal, log_message)
}
#[inline]
pub fn log_error(log_message: &str) -> Option<MessageId> {
log(LogLevel::Error, log_message)
}
#[inline]
pub fn log_warning(log_message: &str) -> Option<MessageId> {
log(LogLevel::Warning, log_message)
}
#[inline]
pub fn log_info(log_message: &str) -> Option<MessageId> {
log(LogLevel::Info, log_message)
}
pub fn log_debug(log_message: &str) -> Option<MessageId> {
log(LogLevel::Debug, log_message)
}