Skip to content

Commit d1397ed

Browse files
committed
Enable status-code aware logging
1 parent b780d8c commit d1397ed

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

src/log/middleware.rs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,45 @@ impl LogMiddleware {
3131
) -> crate::Result {
3232
let path = ctx.uri().path().to_owned();
3333
let method = ctx.method().to_string();
34-
log::trace!("IN => {} {}", method, path);
34+
log::info!("<-- Request received", {
35+
method: method,
36+
path: path,
37+
});
3538
let start = std::time::Instant::now();
3639
match next.run(ctx).await {
3740
Ok(res) => {
3841
let status = res.status();
39-
log::info!(
40-
"{} {} {} {}ms",
41-
method,
42-
path,
43-
status,
44-
start.elapsed().as_millis()
45-
);
42+
if status.is_server_error() {
43+
log::error!("--> Response sent", {
44+
method: method,
45+
path: path,
46+
status: status as u16,
47+
duration: format!("{}ms", start.elapsed().as_millis()),
48+
});
49+
} else if status.is_client_error() {
50+
log::warn!("--> Response sent", {
51+
method: method,
52+
path: path,
53+
status: status as u16,
54+
duration: format!("{}ms", start.elapsed().as_millis()),
55+
});
56+
} else {
57+
log::info!("--> Response sent", {
58+
method: method,
59+
path: path,
60+
status: status as u16,
61+
duration: format!("{}ms", start.elapsed().as_millis()),
62+
});
63+
}
4664
Ok(res)
4765
}
4866
Err(err) => {
49-
let msg = err.to_string();
50-
log::error!(
51-
"{} {} {} {}ms",
52-
msg,
53-
method,
54-
path,
55-
start.elapsed().as_millis()
56-
);
67+
log::error!("{}", err.to_string(), {
68+
method: method,
69+
path: path,
70+
status: err.status() as u16,
71+
duration: format!("{}ms", start.elapsed().as_millis()),
72+
});
5773
Err(err)
5874
}
5975
}

0 commit comments

Comments
 (0)