From 7a5a5a73a92df73d0c74f1bc453fd416cec532e5 Mon Sep 17 00:00:00 2001 From: prk-Jr Date: Wed, 21 Jan 2026 21:57:26 +0530 Subject: [PATCH] Standardize logging: Remove manual prefixes, add module target to logger format Uses record.target() in the logger to automatically include module names in brackets, eliminating inconsistent manual prefixes that caused duplicate context like '[proxy] proxy: message'. --- crates/common/src/proxy.rs | 39 ++++++++---------------- crates/common/src/streaming_processor.rs | 29 ++++++++---------- crates/fastly/src/main.rs | 7 ++++- 3 files changed, 32 insertions(+), 43 deletions(-) diff --git a/crates/common/src/proxy.rs b/crates/common/src/proxy.rs index 64b72bc..c4abd6d 100644 --- a/crates/common/src/proxy.rs +++ b/crates/common/src/proxy.rs @@ -137,7 +137,7 @@ fn finalize_proxied_response( let ct_for_log: &str = if ct_raw.is_empty() { "-" } else { &ct_raw }; log::info!( - "proxy: origin response status={} ct={} cl={} accept={} url={}", + "origin response status={} ct={} cl={} accept={} url={}", status_code, ct_for_log, cl_raw, @@ -199,11 +199,7 @@ fn finalize_proxied_response( } if is_pixel { - log::info!( - "proxy: likely pixel image fetched: {} ct={}", - target_url, - ct - ); + log::info!("likely pixel image fetched: {} ct={}", target_url, ct); } return beresp; @@ -235,7 +231,7 @@ fn finalize_proxied_response_streaming( let ct_for_log: &str = if ct_raw.is_empty() { "-" } else { &ct_raw }; log::info!( - "proxy(stream): origin response status={} ct={} cl={} accept={} url={}", + "stream: origin response status={} ct={} cl={} accept={} url={}", status_code, ct_for_log, cl_raw, @@ -280,7 +276,7 @@ fn finalize_proxied_response_streaming( if is_pixel { log::info!( - "proxy(stream): likely pixel image fetched: {} ct={}", + "stream: likely pixel image fetched: {} ct={}", target_url, ct ); @@ -340,10 +336,7 @@ fn append_synthetic_id(req: &Request, target_url_parsed: &mut url::Url) { let synthetic_id_param = match get_synthetic_id(req) { Ok(id) => id, Err(e) => { - log::warn!( - "proxy: failed to extract synthetic ID for forwarding: {:?}", - e - ); + log::warn!("failed to extract synthetic ID for forwarding: {:?}", e); None } }; @@ -368,11 +361,11 @@ fn append_synthetic_id(req: &Request, target_url_parsed: &mut url::Url) { } log::debug!( - "proxy: forwarding synthetic_id to origin url {}", + "forwarding synthetic_id to origin url {}", target_url_parsed.as_str() ); } else { - log::debug!("proxy: no synthetic_id to forward to origin"); + log::debug!("no synthetic_id to forward to origin"); } } @@ -469,7 +462,7 @@ async fn proxy_with_redirects( if redirect_attempt == MAX_REDIRECTS { log::warn!( - "proxy: redirect limit reached for {}; returning redirect response", + "redirect limit reached for {}; returning redirect response", current_url ); return Ok(finalize_proxied_response( @@ -498,7 +491,7 @@ async fn proxy_with_redirects( } log::info!( - "proxy: following redirect {} => {} (status {})", + "following redirect {} => {} (status {})", current_url, next_url, status.as_u16() @@ -569,10 +562,7 @@ pub async fn handle_first_party_click( let synthetic_id = match get_synthetic_id(&req) { Ok(id) => id, Err(e) => { - log::warn!( - "click: failed to extract synthetic ID for forwarding: {:?}", - e - ); + log::warn!("failed to extract synthetic ID for forwarding: {:?}", e); None } }; @@ -599,15 +589,12 @@ pub async fn handle_first_party_click( } let final_target = url.to_string(); - log::debug!( - "click: forwarding synthetic_id to target url {}", - final_target - ); + log::debug!("forwarding synthetic_id to target url {}", final_target); redirect_target = final_target; } Err(e) => { log::warn!( - "click: failed to parse target url for synthetic forwarding: {:?}", + "failed to parse target url for synthetic forwarding: {:?}", e ); } @@ -624,7 +611,7 @@ pub async fn handle_first_party_click( .and_then(|h| h.to_str().ok()) .unwrap_or(""); log::info!( - "click: redirect tsurl={} params_present={} target={} referer={} ua={} synthetic_id={}", + "redirect tsurl={} params_present={} target={} referer={} ua={} synthetic_id={}", tsurl, had_params, redirect_target, diff --git a/crates/common/src/streaming_processor.rs b/crates/common/src/streaming_processor.rs index eb5d608..80bf7a4 100644 --- a/crates/common/src/streaming_processor.rs +++ b/crates/common/src/streaming_processor.rs @@ -185,7 +185,7 @@ impl StreamingPipeline

{ message: "Failed to decompress gzip".to_string(), })?; - log::info!("[Gzip] Decompressed size: {} bytes", decompressed.len()); + log::info!("Decompressed size: {} bytes", decompressed.len()); // Process the decompressed content let processed = self @@ -195,7 +195,7 @@ impl StreamingPipeline

{ message: "Failed to process content".to_string(), })?; - log::info!("[Gzip] Processed size: {} bytes", processed.len()); + log::info!("Processed size: {} bytes", processed.len()); // Recompress the output let mut encoder = GzEncoder::new(output, Compression::default()); @@ -228,10 +228,7 @@ impl StreamingPipeline

{ message: "Failed to decompress gzip".to_string(), })?; - log::info!( - "[Gzip->None] Decompressed size: {} bytes", - decompressed.len() - ); + log::info!("Gzip->None decompressed size: {} bytes", decompressed.len()); // Process the decompressed content let processed = self @@ -241,7 +238,7 @@ impl StreamingPipeline

{ message: "Failed to process content".to_string(), })?; - log::info!("[Gzip->None] Processed size: {} bytes", processed.len()); + log::info!("Gzip->None processed size: {} bytes", processed.len()); // Write uncompressed output output @@ -287,7 +284,7 @@ impl StreamingPipeline

{ })?; log::info!( - "[Deflate->None] Decompressed size: {} bytes", + "Deflate->None decompressed size: {} bytes", decompressed.len() ); @@ -299,7 +296,7 @@ impl StreamingPipeline

{ message: "Failed to process content".to_string(), })?; - log::info!("[Deflate->None] Processed size: {} bytes", processed.len()); + log::info!("Deflate->None processed size: {} bytes", processed.len()); // Write uncompressed output output @@ -350,7 +347,7 @@ impl StreamingPipeline

{ })?; log::info!( - "[Brotli->None] Decompressed size: {} bytes", + "Brotli->None decompressed size: {} bytes", decompressed.len() ); @@ -362,7 +359,7 @@ impl StreamingPipeline

{ message: "Failed to process content".to_string(), })?; - log::info!("[Brotli->None] Processed size: {} bytes", processed.len()); + log::info!("Brotli->None processed size: {} bytes", processed.len()); // Write uncompressed output output @@ -462,7 +459,7 @@ impl StreamProcessor for HtmlRewriterAdapter { if !chunk.is_empty() { log::debug!( - "[HtmlRewriter] Buffering chunk: {} bytes, total buffered: {} bytes", + "Buffering chunk: {} bytes, total buffered: {} bytes", chunk.len(), self.accumulated_input.len() ); @@ -471,7 +468,7 @@ impl StreamProcessor for HtmlRewriterAdapter { // Only process when we have all the input if is_last { log::info!( - "[HtmlRewriter] Processing complete document: {} bytes", + "Processing complete document: {} bytes", self.accumulated_input.len() ); @@ -488,17 +485,17 @@ impl StreamProcessor for HtmlRewriterAdapter { // Process the entire document rewriter.write(&self.accumulated_input).map_err(|e| { - log::error!("[HtmlRewriter] Failed to process HTML: {}", e); + log::error!("Failed to process HTML: {}", e); io::Error::other(format!("HTML processing failed: {}", e)) })?; // Finalize the rewriter rewriter.end().map_err(|e| { - log::error!("[HtmlRewriter] Failed to finalize: {}", e); + log::error!("Failed to finalize: {}", e); io::Error::other(format!("HTML finalization failed: {}", e)) })?; - log::debug!("[HtmlRewriter] Output size: {} bytes", output.len()); + log::debug!("Output size: {} bytes", output.len()); self.accumulated_input.clear(); Ok(output) } else { diff --git a/crates/fastly/src/main.rs b/crates/fastly/src/main.rs index 183fea9..ac74467 100644 --- a/crates/fastly/src/main.rs +++ b/crates/fastly/src/main.rs @@ -131,9 +131,14 @@ fn init_logger() { fern::Dispatch::new() .format(|out, message, record| { out.finish(format_args!( - "{} {} {}", + "{} {} [{}] {}", chrono::Local::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true), record.level(), + record + .target() + .split("::") + .last() + .unwrap_or(record.target()), message )) })