From f1764bba435e525df5cc06ea0c3972c7317e7078 Mon Sep 17 00:00:00 2001 From: Mohab Usama Date: Fri, 31 May 2019 10:09:21 +0200 Subject: [PATCH] Fix Logger time format (use rfc3339) (#867) * Fix Logger time format (use rfc3339) * Update change log --- CHANGES.md | 2 ++ src/middleware/logger.rs | 32 +++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 5974ee69..0dc7be27 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -15,6 +15,8 @@ ### Fixed +* Fix Logger request time format, and use rfc3339. #867 + * Clear http requests pool on app service drop #860 diff --git a/src/middleware/logger.rs b/src/middleware/logger.rs index 5d0b615e..d47e4502 100644 --- a/src/middleware/logger.rs +++ b/src/middleware/logger.rs @@ -53,7 +53,7 @@ use crate::HttpResponse; /// /// `%a` Remote IP-address (IP-address of proxy if using reverse proxy) /// -/// `%t` Time when the request was started to process +/// `%t` Time when the request was started to process (in rfc3339 format) /// /// `%r` First line of request /// @@ -417,10 +417,7 @@ impl FormatText { } FormatText::UrlPath => *self = FormatText::Str(format!("{}", req.path())), FormatText::RequestTime => { - *self = FormatText::Str(format!( - "{:?}", - now.strftime("[%d/%b/%Y:%H:%M:%S %z]").unwrap() - )) + *self = FormatText::Str(format!("{}", now.rfc3339())) } FormatText::RequestHeader(ref name) => { let s = if let Some(val) = req.headers().get(name) { @@ -547,4 +544,29 @@ mod tests { assert!(s.contains("200 1024")); assert!(s.contains("ACTIX-WEB")); } + + #[test] + fn test_request_time_format() { + let mut format = Format::new("%t"); + let req = TestRequest::default().to_srv_request(); + + let now = time::now(); + for unit in &mut format.0 { + unit.render_request(now, &req); + } + + let resp = HttpResponse::build(StatusCode::OK).force_close().finish(); + for unit in &mut format.0 { + unit.render_response(&resp); + } + + let render = |fmt: &mut Formatter| { + for unit in &format.0 { + unit.render(fmt, 1024, now)?; + } + Ok(()) + }; + let s = format!("{}", FormatDisplay(&render)); + assert!(s.contains(&format!("{}", now.rfc3339()))); + } }