redup_bootstrap/telemetry/journal/
mod.rs1mod event_data;
8mod fatal;
9mod human;
10mod json;
11mod layer;
12mod panic_hook;
13mod row;
14mod span_context;
15mod state;
16#[cfg(test)]
17mod tests;
18mod timestamp;
19
20pub(crate) use fatal::fatal;
21
22use std::sync::Arc;
23#[cfg(test)]
24use std::sync::Mutex;
25
26use time::OffsetDateTime;
27use tokio::sync::broadcast;
28
29use crate::telemetry::level::Level;
30use crate::telemetry::logs_config::LogsConfig;
31use crate::telemetry::service::Service;
32use crate::telemetry::template::Template;
33
34pub(crate) type Clock = Arc<dyn Fn() -> OffsetDateTime + Send + Sync>;
36
37pub(crate) fn system_clock() -> Clock {
39 Arc::new(OffsetDateTime::now_utc)
40}
41
42pub(crate) const FATAL: &str = "fatal";
44
45pub(crate) const CORRELATION_FIELD: &str = "correlation_id";
47pub(crate) const TRACE_FIELD: &str = "trace_id";
49pub(crate) const SPAN_FIELD: &str = "span_id";
51
52pub(crate) enum Output {
54 Stdout,
56 #[cfg(test)]
58 Buffer(Arc<Mutex<Vec<u8>>>),
59}
60
61#[derive(Debug, Clone, PartialEq, Eq)]
63pub struct LogMessage {
64 timestamp: OffsetDateTime,
65 level: Level,
66 message: String,
67 correlation_id: Option<String>,
68 exception: Option<String>,
69}
70
71impl LogMessage {
72 pub fn timestamp(&self) -> OffsetDateTime {
74 self.timestamp
75 }
76
77 pub fn level(&self) -> Level {
79 self.level
80 }
81
82 pub fn message(&self) -> &str {
84 &self.message
85 }
86
87 pub fn correlation_id(&self) -> Option<&str> {
89 self.correlation_id.as_deref()
90 }
91
92 pub fn exception(&self) -> Option<&str> {
94 self.exception.as_deref()
95 }
96}
97
98pub struct Capture {
100 receiver: tokio::sync::broadcast::Receiver<LogMessage>,
102 min_level: Level,
104}
105
106impl Capture {
107 pub async fn next(&mut self) -> Option<LogMessage> {
110 loop {
111 match self.receiver.recv().await {
112 Ok(message) if message.level >= self.min_level => return Some(message),
113 Ok(_) => continue,
114 Err(broadcast::error::RecvError::Lagged(_)) => continue,
115 Err(broadcast::error::RecvError::Closed) => return None,
116 }
117 }
118 }
119}
120
121#[derive(Clone)]
123pub struct Logging {
124 pub(crate) state: Arc<state::State>,
126}
127
128#[derive(Clone)]
130pub(crate) struct Journal {
131 pub(crate) state: Arc<state::State>,
133}
134
135impl Journal {
136 pub(crate) fn new(config: LogsConfig, service: Service, output: Output, clock: Clock) -> Self {
139 Self {
140 state: Arc::new(state::State::new(&config, &service, output, clock)),
141 }
142 }
143
144 pub(crate) fn handle(&self) -> Logging {
146 Logging {
147 state: Arc::clone(&self.state),
148 }
149 }
150}
151
152impl Logging {
153 pub fn level(&self) -> Level {
155 self.state.level()
156 }
157
158 pub fn set_level(&self, level: Level) {
160 self.state.set_level(level);
161 }
162
163 pub fn template(&self) -> Template {
165 self.state.template()
166 }
167
168 pub fn set_template(&self, template: Template) {
170 self.state.set_template(template);
171 }
172
173 pub fn capture(&self, min_level: Level) -> Capture {
175 Capture {
176 receiver: self.state.subscribe(),
177 min_level,
178 }
179 }
180}
181
182pub(crate) fn set_panic_hook() {
184 panic_hook::install();
185}