Skip to content

Commit

Permalink
fix: log middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
NewbMiao committed Jun 8, 2023
1 parent d13b5c5 commit 7717c75
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async fn main() {
let subscriber = tracing_subscriber::fmt()
.compact()
.with_max_level(tracing::Level::INFO)
.with_file(true)
.with_file(false)
.with_line_number(true)
.with_thread_ids(true)
.with_target(false)
Expand Down
12 changes: 6 additions & 6 deletions src/middlewares/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ pub async fn log(
let (req_parts, req_body) = req.into_parts();

// Print request
let bytes = buffer_and_print("request", path, req_body, no_log).await?;
let bytes = buffer_and_print("Request", path, req_body, no_log).await?;
let req = Request::from_parts(req_parts, hyper::Body::from(bytes));

let res = next.run(req).await;

let (mut res_parts, res_body) = res.into_parts();

// Print response
let bytes = buffer_and_print("response", path, res_body, no_log).await?;
let bytes = buffer_and_print("Response", path, res_body, no_log).await?;

// When your encoding is chunked there can be problems without removing the header
res_parts.headers.remove("transfer-encoding");
Expand All @@ -48,7 +48,7 @@ async fn buffer_and_print<B>(
direction: &str,
path: &str,
body: B,
log: bool,
no_log: bool,
) -> Result<Bytes, (StatusCode, String)>
where
B: axum::body::HttpBody<Data = Bytes>,
Expand All @@ -65,16 +65,16 @@ where
};

if let Ok(body) = std::str::from_utf8(&bytes) {
if log && !body.is_empty() {
if !no_log && !body.is_empty() {
if body.len() > 2000 {
info!(
"{} for req: {} with body: {}...",
"{} for {} with body: {}...",
direction,
path,
&body[0..2000]
);
} else {
info!("{} for req: {} with body: {}", direction, path, body);
info!("{} for {} with body: {}", direction, path, body);
}
}
}
Expand Down

0 comments on commit 7717c75

Please sign in to comment.