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
use std::sync::{Arc, Mutex};
use chrono::Utc;
use serde_json::{Map, Value};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Level {
Debug,
Info,
Warn,
Error,
}
impl Level {
pub fn as_str(self) -> &'static str {
match self {
Self::Debug => "debug",
Self::Info => "info",
Self::Warn => "warn",
Self::Error => "error",
}
}
pub fn parse_level(value: &str) -> Self {
match value.to_ascii_lowercase().as_str() {
"debug" => Self::Debug,
"warn" | "warning" => Self::Warn,
"error" => Self::Error,
_ => Self::Info,
}
}
}
pub trait LogSink: Send + Sync {
fn write_line(&self, line: &str);
}
pub struct StderrSink;
impl LogSink for StderrSink {
fn write_line(&self, line: &str) {
eprintln!("{line}");
}
}
#[derive(Default)]
pub struct CaptureSink {
lines: Mutex<Vec<String>>,
}
impl CaptureSink {
pub fn lines(&self) -> Vec<String> {
self.lines.lock().expect("capture sink poisoned").clone()
}
}
impl LogSink for CaptureSink {
fn write_line(&self, line: &str) {
self.lines
.lock()
.expect("capture sink poisoned")
.push(line.to_owned());
}
}
#[derive(Clone)]
pub struct Logger {
sink: Arc<dyn LogSink>,
min_level: Level,
target: String,
}
impl Logger {
pub fn new(sink: Arc<dyn LogSink>, min_level: Level) -> Self {
Self {
sink,
min_level,
target: "vuln-notif".to_owned(),
}
}
pub fn stderr(min_level: Level) -> Self {
Self::new(Arc::new(StderrSink), min_level)
}
pub fn with_target(mut self, target: impl Into<String>) -> Self {
self.target = target.into();
self
}
pub fn debug(&self, event: &str, scope: Option<&str>, fields: Value) {
self.log(Level::Debug, event, scope, fields);
}
pub fn info(&self, event: &str, scope: Option<&str>, fields: Value) {
self.log(Level::Info, event, scope, fields);
}
pub fn warn(&self, event: &str, scope: Option<&str>, fields: Value) {
self.log(Level::Warn, event, scope, fields);
}
pub fn error(&self, event: &str, scope: Option<&str>, fields: Value) {
self.log(Level::Error, event, scope, fields);
}
pub fn log(&self, level: Level, event: &str, scope: Option<&str>, fields: Value) {
if level < self.min_level {
return;
}
let mut map = Map::new();
map.insert(
"ts".into(),
Value::String(Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true)),
);
map.insert("level".into(), Value::String(level.as_str().into()));
map.insert("target".into(), Value::String(self.target.clone()));
map.insert("event".into(), Value::String(event.into()));
if let Some(scope) = scope {
map.insert("scope".into(), Value::String(scope.into()));
}
if let Value::Object(fields) = fields {
for (key, value) in fields {
if matches!(key.as_str(), "ts" | "level" | "target" | "event" | "scope") {
continue;
}
map.insert(key, value);
}
}
self.sink.write_line(&Value::Object(map).to_string());
}
}