Add multiline option

This commit is contained in:
Michael Pfaff 2021-01-21 12:06:22 -05:00
parent c15c901a5c
commit 338613dfc1
Signed by: michael
GPG Key ID: E53B118B12B5C7F9
3 changed files with 32 additions and 13 deletions

2
Cargo.lock generated
View File

@ -1364,7 +1364,7 @@ dependencies = [
[[package]]
name = "tide_tracing"
version = "0.2.0"
version = "0.3.0"
dependencies = [
"async-trait",
"chrono",

View File

@ -1,6 +1,6 @@
[package]
name = "tide_tracing"
version = "0.2.0"
version = "0.3.0"
authors = ["Michael Pfaff <michael@pfaff.dev>"]
edition = "2018"

View File

@ -13,6 +13,7 @@ use tracing::Level;
pub struct LogMiddleware {
pub include_ip: bool,
pub include_query: bool,
pub multiline: bool,
}
impl Default for LogMiddleware {
@ -20,6 +21,7 @@ impl Default for LogMiddleware {
Self {
include_ip: false,
include_query: false,
multiline: false,
}
}
}
@ -36,26 +38,43 @@ impl LogMiddleware {
let ip = req.remote().unwrap_or("unknown").to_string();
let span = span!(Level::ERROR, "Request", id = %id);
let res = async {
debug!(path = uri.path());
let path = uri.path();
let method = req.method();
let query = uri.query().unwrap_or("");
let received = start.to_rfc3339_opts(chrono::SecondsFormat::AutoSi, false);
debug!(method = %req.method());
if self.multiline {
debug!(path = path);
if self.include_query {
debug!(query = uri.query().unwrap_or(""));
debug!(method = %method);
if self.include_ip {
debug!(client_ip = %ip);
}
debug!(received = received);
if self.include_query {
debug!(query = query);
}
} else {
debug!(path = path, method = %method, client_ip = %ip, received = received, query = query);
}
if self.include_ip {
debug!(client_ip = %ip);
}
debug!(received = %start);
// debug!(method = %req.method(), query = uri.query().unwrap_or(""), client_ip = %ip, received = %start, "Request recieved");
let res = next.run(req).await;
let end = Utc::now();
let span = span!(Level::ERROR, "Response");
async {
debug!(status = %res.status());
debug!(duration = %end.signed_duration_since(start));
let status = res.status();
let duration = end.signed_duration_since(start);
if self.multiline {
debug!(status = %status);
debug!(duration = %duration);
} else {
debug!(status = %status, duration = %duration);
}
}.instrument(span).await;
res
}.instrument(span).await;