1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use log::Record;
use chrono::{Local};
use std::io::Write;
pub fn json_format<W>(buf: &mut W, record: &Record) -> std::io::Result<()>
    where
        W: Write,
{
    let timestamp = Local::now().format("%Y-%m-%dT%H:%M:%S%.3f%:z").to_string();
    let level = record.level().to_string().to_lowercase();
    let message = record.args().to_string();
    let target = record.target();
    let file = record.file().unwrap_or("");
    let line = record.line().map_or("".to_string(), |l| format!("{}:{}", file, l));
    let json = format!(
        r#"{{"level":"{}","timestamp":"{}","msg":"{}","line":"{}","target":"{}"}}"#,
        level,
        timestamp,
        message,
        line,
        target
    );
    writeln!(buf, "{}", json)
}