-
Notifications
You must be signed in to change notification settings - Fork 20
/
json_logger.rs
81 lines (71 loc) · 2.17 KB
/
json_logger.rs
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
use log::{kv, LevelFilter, Log, Metadata, Record};
use std::io::{self, StdoutLock, Write};
use std::time;
/// Start logging.
pub fn start(level: LevelFilter) {
let logger = Box::new(Logger {});
log::set_boxed_logger(logger).expect("Could not start logging");
log::set_max_level(level);
}
#[derive(Debug)]
pub(crate) struct Logger {}
impl Log for Logger {
fn enabled(&self, metadata: &Metadata<'_>) -> bool {
metadata.level() <= log::max_level()
}
fn log(&self, record: &Record<'_>) {
if self.enabled(record.metadata()) {
let stdout = io::stdout();
let mut handle = stdout.lock();
let level = get_level(record.level());
let time = time::UNIX_EPOCH.elapsed().unwrap().as_millis();
write!(
&mut handle,
"{{\"level\":{},\"time\":{},\"msg\":",
level, time
)
.unwrap();
serde_json::to_writer(&mut handle, record.args()).unwrap();
format_kv_pairs(&mut handle, record);
writeln!(&mut handle, "}}").unwrap();
}
}
fn flush(&self) {}
}
fn get_level(level: log::Level) -> u8 {
use log::Level::*;
match level {
Trace => 10,
Debug => 20,
Info => 30,
Warn => 40,
Error => 50,
}
}
fn format_kv_pairs(out: &mut StdoutLock<'_>, record: &Record) {
struct Visitor<'a, 'b> {
string: &'a mut StdoutLock<'b>,
}
impl<'kvs, 'a, 'b> kv::Visitor<'kvs> for Visitor<'a, 'b> {
fn visit_pair(
&mut self,
key: kv::Key<'kvs>,
val: kv::Value<'kvs>,
) -> Result<(), kv::Error> {
if let Ok(value_str) = serde_json::to_string(&val) {
write!(self.string, ",\"{}\":{}", key, value_str)?;
} else {
write!(self.string, ",\"{}\":\"{}\"", key, val)?;
}
Ok(())
}
}
let mut visitor = Visitor { string: out };
record.key_values().visit(&mut visitor).unwrap();
}
pub fn make_value<ValueType>(value: &ValueType) -> log::kv::Value<'_>
where
ValueType: serde::Serialize,
{
log::kv::Value::from_serde(value)
}