1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

Implement Logger middleware regex exclude pattern (#1723)

Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
Matt Gathu 2020-10-19 08:18:16 +02:00 committed by GitHub
parent 2f84914146
commit cfd5b381f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 3 deletions

View File

@ -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

View File

@ -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"

View File

@ -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};
@ -92,6 +92,7 @@ pub struct Logger(Rc<Inner>);
struct Inner {
format: Format,
exclude: HashSet<String>,
exclude_regex: RegexSet,
}
impl Logger {
@ -100,6 +101,7 @@ impl Logger {
Logger(Rc::new(Inner {
format: Format::new(format),
exclude: HashSet::new(),
exclude_regex: RegexSet::empty(),
}))
}
@ -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 {
@ -123,6 +135,7 @@ impl Default for Logger {
Logger(Rc::new(Inner {
format: Format::default(),
exclude: HashSet::new(),
exclude_regex: RegexSet::empty(),
}))
}
}
@ -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,
@ -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");