Skip to content

Commit f498957

Browse files
committed
update
1 parent cf93005 commit f498957

3 files changed

Lines changed: 63 additions & 94 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
name = "fastify-pretty-log"
33
version = "0.1.0"
44
edition = "2021"
5+
description = "A simple log formatter for fastify"
6+
license = "MIT"
7+
authors = ["Erfan Safari <erfanshield@outlook.com>"]
58

69
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
710

snap/snapcraft.yaml

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/main.rs

Lines changed: 60 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use serde_json::Value;
44
use std::collections::HashMap;
55
use std::io::{self, BufRead};
66
use termion::color;
7-
87
#[derive(Deserialize)]
98
struct ReqLog {
109
req: Req,
@@ -35,37 +34,20 @@ struct Res {
3534
}
3635

3736
fn colorize_status_code(status_code: u64) -> String {
38-
if status_code >= 500 {
39-
format!(
40-
"{}{}{}",
41-
color::Fg(color::Red),
42-
status_code,
43-
color::Fg(color::Reset)
44-
)
45-
} else if status_code >= 400 {
46-
format!(
47-
"{}{}{}",
48-
color::Fg(color::Yellow),
49-
status_code,
50-
color::Fg(color::Reset)
51-
)
52-
} else if status_code >= 300 {
53-
format!(
54-
"{}{}{}",
55-
color::Fg(color::Blue),
56-
status_code,
57-
color::Fg(color::Reset)
58-
)
59-
} else if status_code >= 200 {
60-
format!(
61-
"{}{}{}",
62-
color::Fg(color::Green),
63-
status_code,
64-
color::Fg(color::Reset)
65-
)
66-
} else {
67-
status_code.to_string()
68-
}
37+
let color: Box<dyn color::Color> = match status_code {
38+
200 => Box::new(color::Green),
39+
300 => Box::new(color::Blue),
40+
400 => Box::new(color::Yellow),
41+
500 => Box::new(color::Red),
42+
_ => Box::new(color::White),
43+
};
44+
45+
format!(
46+
"{}{}{}",
47+
color::Fg(&*color),
48+
status_code,
49+
color::Fg(color::Reset)
50+
)
6951
}
7052

7153
fn main() {
@@ -77,63 +59,63 @@ fn main() {
7759
let value: Result<Value, _> = serde_json::from_str(&line);
7860

7961
if let Ok(value) = value {
80-
if let Some(req_id_value) = value.get("reqId") {
81-
let req_id = req_id_value.as_str().unwrap().to_string();
82-
83-
if value.get("req").is_some() {
84-
handle_req_log(value, &req_id, &mut req_logs);
85-
} else if value.get("res").is_some() {
86-
handle_res_log(value, &req_id, &mut req_logs);
87-
}
88-
} else {
89-
let message_to_log = match value.get("msg") {
90-
Some(msg) => msg.as_str().unwrap().to_string(),
91-
None => line,
92-
};
93-
94-
println!("{}", message_to_log);
95-
}
62+
handle_json_line(value, &mut req_logs, line);
9663
} else {
9764
println!("{}", line);
9865
}
9966
}
10067
}
10168

102-
fn handle_req_log(value: Value, req_id: &str, req_logs: &mut HashMap<String, ReqLog>) {
103-
let log: ReqLog = serde_json::from_value(value).unwrap();
104-
req_logs.insert(req_id.to_string(), log);
69+
fn handle_json_line(value: Value, req_logs: &mut HashMap<String, ReqLog>, raw_line: String) {
70+
if let Some(req_id_value) = value.get("reqId") {
71+
let req_id = req_id_value.as_str().unwrap().to_string();
72+
73+
if value.get("req").is_some() {
74+
let log: ReqLog = serde_json::from_value(value).unwrap();
75+
req_logs.insert(req_id.to_string(), log);
76+
} else if value.get("res").is_some() {
77+
req_logs.get(&req_id);
78+
if let Some(req_log) = req_logs.get(&req_id) {
79+
handle_res_log(req_log, value);
80+
}
81+
req_logs.remove(&req_id);
82+
}
83+
} else {
84+
let message_to_log = match value.get("msg") {
85+
Some(msg) => msg.as_str().unwrap().to_string(),
86+
None => raw_line,
87+
};
88+
89+
println!("{}", message_to_log);
90+
}
10591
}
10692

107-
fn handle_res_log(value: Value, req_id: &str, req_logs: &mut HashMap<String, ReqLog>) {
93+
fn handle_res_log(req_log: &ReqLog, value: Value) {
10894
let log: ResLog = serde_json::from_value(value).unwrap();
109-
if let Some(req_log) = req_logs.get(req_id) {
110-
let response_time = match log.responseTime {
111-
Some(time) => format!("{:.3}ms", time),
112-
None => "N/A".to_string(),
113-
};
114-
let status_code = log.res.statusCode;
95+
let response_time = match log.responseTime {
96+
Some(time) => format!("{:.3}ms", time),
97+
None => "N/A".to_string(),
98+
};
99+
let status_code = log.res.statusCode;
115100

101+
if let Some(err) = &log.err {
116102
println!(
117-
"{} {} {} {}",
118-
req_log.req.url,
119-
req_log.req.method,
120-
colorize_status_code(status_code),
121-
response_time
103+
"{}\n{}",
104+
format!(
105+
"{}{}{}",
106+
color::Fg(color::Red),
107+
err.message,
108+
color::Fg(color::Reset)
109+
),
110+
err.stack
122111
);
123-
124-
if let Some(err) = &log.err {
125-
println!(
126-
"{}\n{}",
127-
format!(
128-
"{}{}{}",
129-
color::Fg(color::Red),
130-
err.message,
131-
color::Fg(color::Reset)
132-
),
133-
err.stack
134-
);
135-
}
136-
137-
req_logs.remove(req_id);
138112
}
113+
114+
println!(
115+
"{} {} {} {}",
116+
req_log.req.url,
117+
req_log.req.method,
118+
colorize_status_code(status_code),
119+
response_time
120+
);
139121
}

0 commit comments

Comments
 (0)