From cb7347216ce1d715fd4fa6c3e5d348038595b06f Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Fri, 21 Jan 2022 17:18:39 +0000 Subject: [PATCH] add `Logger::log_target` (#2594) --- CHANGES.md | 4 +++- examples/basic.rs | 2 +- src/middleware/logger.rs | 43 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 44bbc30f9..1837aa577 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,8 @@ ## Unreleased - 2021-xx-xx ### Added -- `HttpResponse::add_removal_cookie` [#2586] +- `HttpResponse::add_removal_cookie`. [#2586] +- `Logger::log_target`. [#2594] ### Removed - `HttpRequest::req_data[_mut]()`; request-local data is still available through `.extensions()`. [#2585] @@ -11,6 +12,7 @@ [#2585]: https://github.com/actix/actix-web/pull/2585 [#2586]: https://github.com/actix/actix-web/pull/2586 [#2591]: https://github.com/actix/actix-web/pull/2591 +[#2594]: https://github.com/actix/actix-web/pull/2594 ## 4.0.0-beta.20 - 2022-01-14 diff --git a/examples/basic.rs b/examples/basic.rs index 494470676..36b1cdd8f 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -24,7 +24,7 @@ async fn main() -> std::io::Result<()> { App::new() .wrap(middleware::DefaultHeaders::new().add(("X-Version", "0.2"))) .wrap(middleware::Compress::default()) - .wrap(middleware::Logger::default().log_target("1234")) + .wrap(middleware::Logger::default().log_target("http_log")) .service(index) .service(no_params) .service( diff --git a/src/middleware/logger.rs b/src/middleware/logger.rs index d68e1a122..53a3550de 100644 --- a/src/middleware/logger.rs +++ b/src/middleware/logger.rs @@ -1,6 +1,7 @@ //! For middleware documentation, see [`Logger`]. use std::{ + borrow::Cow, collections::HashSet, convert::TryFrom, env, @@ -87,6 +88,7 @@ struct Inner { format: Format, exclude: HashSet, exclude_regex: RegexSet, + log_target: Cow<'static, str>, } impl Logger { @@ -96,6 +98,7 @@ impl Logger { format: Format::new(format), exclude: HashSet::new(), exclude_regex: RegexSet::empty(), + log_target: Cow::Borrowed(module_path!()), })) } @@ -118,6 +121,24 @@ impl Logger { self } + /// Sets the logging target to `target`. + /// + /// By default, the log target is `module_path!()` of the log call location. In our case, that + /// would be `actix_web::middleware::logger`. + /// + /// # Examples + /// Using `.log_target("http_log")` would have this effect on request logs: + /// ```diff + /// - [2015-10-21T07:28:00Z INFO actix_web::middleware::logger] 127.0.0.1 "GET / HTTP/1.1" 200 88 "-" "dmc/1.0" 0.001985 + /// + [2015-10-21T07:28:00Z INFO http_log] 127.0.0.1 "GET / HTTP/1.1" 200 88 "-" "dmc/1.0" 0.001985 + /// ^^^^^^^^ + /// ``` + pub fn log_target(mut self, target: impl Into>) -> Self { + let inner = Rc::get_mut(&mut self.0).unwrap(); + inner.log_target = target.into(); + self + } + /// Register a function that receives a ServiceRequest and returns a String for use in the /// log line. The label passed as the first argument should match a replacement substring in /// the logger format like `%{label}xi`. @@ -171,6 +192,7 @@ impl Default for Logger { format: Format::default(), exclude: HashSet::new(), exclude_regex: RegexSet::empty(), + log_target: Cow::Borrowed(module_path!()), })) } } @@ -222,13 +244,15 @@ where actix_service::forward_ready!(service); fn call(&self, req: ServiceRequest) -> Self::Future { - if self.inner.exclude.contains(req.path()) - || self.inner.exclude_regex.is_match(req.path()) - { + let excluded = self.inner.exclude.contains(req.path()) + || self.inner.exclude_regex.is_match(req.path()); + + if excluded { LoggerResponse { fut: self.service.call(req), format: None, time: OffsetDateTime::now_utc(), + log_target: Cow::Borrowed(""), _phantom: PhantomData, } } else { @@ -238,10 +262,12 @@ where for unit in &mut format.0 { unit.render_request(now, &req); } + LoggerResponse { fut: self.service.call(req), format: Some(format), time: now, + log_target: self.inner.log_target.clone(), _phantom: PhantomData, } } @@ -258,6 +284,7 @@ pin_project! { fut: S::Future, time: OffsetDateTime, format: Option, + log_target: Cow<'static, str>, _phantom: PhantomData, } } @@ -289,12 +316,14 @@ where let time = *this.time; let format = this.format.take(); + let log_target = this.log_target.clone(); Poll::Ready(Ok(res.map_body(move |_, body| StreamLog { body, time, format, size: 0, + log_target, }))) } } @@ -306,7 +335,9 @@ pin_project! { format: Option, size: usize, time: OffsetDateTime, + log_target: Cow<'static, str>, } + impl PinnedDrop for StreamLog { fn drop(this: Pin<&mut Self>) { if let Some(ref format) = this.format { @@ -316,7 +347,11 @@ pin_project! { } Ok(()) }; - log::info!("{}", FormatDisplay(&render)); + + log::info!( + target: this.log_target.as_ref(), + "{}", FormatDisplay(&render) + ); } } }