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
  | 
use chrono::prelude::*;
use log::{Level, Log, Metadata, Record, SetLoggerError};
use serde_json::json;
pub struct Logger;
impl Log for Logger {
    fn enabled(&self, metadata: &Metadata) -> bool {
        metadata.level() <= Level::Info
    }
    fn log(&self, record: &Record) {
        let file: &str = record.file().unwrap_or("");
        let line: i32 = record.line().map_or(-1, |v| v as i32);
        let module_path: &str = record.module_path().unwrap_or("");
        let ts: String = Utc::now().to_string();
        match record.level() {
            Level::Error => {
                println!(
                    "{}",
                    json!({
                      "ts": ts,
                      "file": file,
                      "line": line,
                      "module_path": module_path,
                      "msg": record.args().to_string(),
                      "level": "error"
                    })
                    .to_string()
                )
            }
            Level::Warn => {
                println!(
                    "{}",
                    json!({
                      "ts": ts,
                      "file": file,
                      "line": line,
                      "module_path": module_path,
                      "msg": record.args().to_string(),
                      "level": "warn"
                    })
                    .to_string()
                )
            }
            Level::Info => {
                println!(
                    "{}",
                    json!({
                      "ts": ts,
                      "file": file,
                      "line": line,
                      "module_path": module_path,
                      "msg": record.args().to_string(),
                      "level": "info"
                    })
                    .to_string()
                )
            }
            Level::Debug => {}
            Level::Trace => {}
        }
    }
    fn flush(&self) {}
}
static _LOGGER: Logger = Logger;
pub fn init() -> Result<(), SetLoggerError> {
    log::set_logger(&_LOGGER)
}
  |