Skip to content

Commit

Permalink
Implement Logger middleware regex exclude pattern (actix#1723)
Browse files Browse the repository at this point in the history
Co-authored-by: Rob Ede <[email protected]>
  • Loading branch information
mattgathu and robjtede authored Oct 19, 2020
1 parent 2f84914 commit cfd5b38
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Changes

## Unreleased - 2020-xx-xx
* Implement Logger middleware regex exclude pattern [#1723]

[#1723]: https://github.com/actix/actix-web/pull/1723

## 3.1.0 - 2020-09-29
### Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ log = "0.4"
mime = "0.3"
socket2 = "0.3"
pin-project = "0.4.17"
regex = "1.3"
regex = "1.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_urlencoded = "0.6.1"
Expand Down
41 changes: 39 additions & 2 deletions src/middleware/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use actix_service::{Service, Transform};
use bytes::Bytes;
use futures_util::future::{ok, Ready};
use log::debug;
use regex::Regex;
use regex::{Regex, RegexSet};
use time::OffsetDateTime;

use crate::dev::{BodySize, MessageBody, ResponseBody};
Expand Down Expand Up @@ -92,6 +92,7 @@ pub struct Logger(Rc<Inner>);
struct Inner {
format: Format,
exclude: HashSet<String>,
exclude_regex: RegexSet,
}

impl Logger {
Expand All @@ -100,6 +101,7 @@ impl Logger {
Logger(Rc::new(Inner {
format: Format::new(format),
exclude: HashSet::new(),
exclude_regex: RegexSet::empty(),
}))
}

Expand All @@ -111,6 +113,16 @@ impl Logger {
.insert(path.into());
self
}

/// Ignore and do not log access info for paths that match regex
pub fn exclude_regex<T: Into<String>>(mut self, path: T) -> Self {
let inner = Rc::get_mut(&mut self.0).unwrap();
let mut patterns = inner.exclude_regex.patterns().to_vec();
patterns.push(path.into());
let regex_set = RegexSet::new(patterns).unwrap();
inner.exclude_regex = regex_set;
self
}
}

impl Default for Logger {
Expand All @@ -123,6 +135,7 @@ impl Default for Logger {
Logger(Rc::new(Inner {
format: Format::default(),
exclude: HashSet::new(),
exclude_regex: RegexSet::empty(),
}))
}
}
Expand Down Expand Up @@ -168,7 +181,9 @@ where
}

fn call(&mut self, req: ServiceRequest) -> Self::Future {
if self.inner.exclude.contains(req.path()) {
if self.inner.exclude.contains(req.path())
|| self.inner.exclude_regex.is_match(req.path())
{
LoggerResponse {
fut: self.service.call(req),
format: None,
Expand Down Expand Up @@ -538,6 +553,28 @@ mod tests {
let _res = srv.call(req).await;
}

#[actix_rt::test]
async fn test_logger_exclude_regex() {
let srv = |req: ServiceRequest| {
ok(req.into_response(
HttpResponse::build(StatusCode::OK)
.header("X-Test", "ttt")
.finish(),
))
};
let logger = Logger::new("%% %{User-Agent}i %{X-Test}o %{HOME}e %D test")
.exclude_regex("\\w");

let mut srv = logger.new_transform(srv.into_service()).await.unwrap();

let req = TestRequest::with_header(
header::USER_AGENT,
header::HeaderValue::from_static("ACTIX-WEB"),
)
.to_srv_request();
let _res = srv.call(req).await.unwrap();
}

#[actix_rt::test]
async fn test_url_path() {
let mut format = Format::new("%T %U");
Expand Down

0 comments on commit cfd5b38

Please sign in to comment.