1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-30 18:34:36 +01:00

Add %U (URLPath) for logger (#752)

* Add %R (Route) for logger

* Requested Updates (Route => URLPath, %R => %U)
This commit is contained in:
Haze 2019-04-03 20:41:42 -04:00 committed by Nikolay Kim
parent cef3dc3586
commit 7d6085ddbd
2 changed files with 42 additions and 2 deletions

View File

@ -6,11 +6,12 @@
* `App::configure()` allow to offload app configuration to different methods * `App::configure()` allow to offload app configuration to different methods
* Added `URLPath` option for logger
* Added `ServiceRequest::app_data()`, returns `Data<T>` * Added `ServiceRequest::app_data()`, returns `Data<T>`
* Added `ServiceFromRequest::app_data()`, returns `Data<T>` * Added `ServiceFromRequest::app_data()`, returns `Data<T>`
### Changed ### Changed
* Move multipart support to actix-multipart crate * Move multipart support to actix-multipart crate

View File

@ -65,6 +65,8 @@ use crate::HttpResponse;
/// ///
/// `%D` Time taken to serve the request, in milliseconds /// `%D` Time taken to serve the request, in milliseconds
/// ///
/// `%U` Request URL
///
/// `%{FOO}i` request.headers['FOO'] /// `%{FOO}i` request.headers['FOO']
/// ///
/// `%{FOO}o` response.headers['FOO'] /// `%{FOO}o` response.headers['FOO']
@ -272,7 +274,7 @@ impl Format {
/// Returns `None` if the format string syntax is incorrect. /// Returns `None` if the format string syntax is incorrect.
pub fn new(s: &str) -> Format { pub fn new(s: &str) -> Format {
log::trace!("Access log format: {}", s); log::trace!("Access log format: {}", s);
let fmt = Regex::new(r"%(\{([A-Za-z0-9\-_]+)\}([ioe])|[atPrsbTD]?)").unwrap(); let fmt = Regex::new(r"%(\{([A-Za-z0-9\-_]+)\}([ioe])|[atPrUsbTD]?)").unwrap();
let mut idx = 0; let mut idx = 0;
let mut results = Vec::new(); let mut results = Vec::new();
@ -300,6 +302,7 @@ impl Format {
"r" => FormatText::RequestLine, "r" => FormatText::RequestLine,
"s" => FormatText::ResponseStatus, "s" => FormatText::ResponseStatus,
"b" => FormatText::ResponseSize, "b" => FormatText::ResponseSize,
"U" => FormatText::UrlPath,
"T" => FormatText::Time, "T" => FormatText::Time,
"D" => FormatText::TimeMillis, "D" => FormatText::TimeMillis,
_ => FormatText::Str(m.as_str().to_owned()), _ => FormatText::Str(m.as_str().to_owned()),
@ -328,6 +331,7 @@ pub enum FormatText {
Time, Time,
TimeMillis, TimeMillis,
RemoteAddr, RemoteAddr,
UrlPath,
RequestHeader(String), RequestHeader(String),
ResponseHeader(String), ResponseHeader(String),
EnvironHeader(String), EnvironHeader(String),
@ -413,6 +417,12 @@ impl FormatText {
)) ))
}; };
} }
FormatText::UrlPath => {
*self = FormatText::Str(format!(
"{}",
req.path()
))
}
FormatText::RequestTime => { FormatText::RequestTime => {
*self = FormatText::Str(format!( *self = FormatText::Str(format!(
"{:?}", "{:?}",
@ -473,6 +483,35 @@ mod tests {
let _res = block_on(srv.call(req)); let _res = block_on(srv.call(req));
} }
#[test]
fn test_url_path() {
let mut format = Format::new("%T %U");
let req = TestRequest::with_header(
header::USER_AGENT,
header::HeaderValue::from_static("ACTIX-WEB"),
).uri("/test/route/yeah").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));
println!("{}", s);
assert!(s.contains("/test/route/yeah"));
}
#[test] #[test]
fn test_default_format() { fn test_default_format() {
let mut format = Format::default(); let mut format = Format::default();