mirror of
https://github.com/actix/actix-web.git
synced 2025-04-14 22:04:06 +00:00
Merge 77c3115568
into 74c1c3de59
This commit is contained in:
commit
2b01f4d5ca
1 changed files with 29 additions and 2 deletions
|
@ -16,7 +16,7 @@ use actix_service::{Service, Transform};
|
|||
use actix_utils::future::{ready, Ready};
|
||||
use bytes::Bytes;
|
||||
use futures_core::ready;
|
||||
use log::{debug, warn};
|
||||
use log::{debug, warn, Level};
|
||||
use pin_project_lite::pin_project;
|
||||
#[cfg(feature = "unicode")]
|
||||
use regex::Regex;
|
||||
|
@ -92,6 +92,7 @@ struct Inner {
|
|||
exclude: HashSet<String>,
|
||||
exclude_regex: Vec<Regex>,
|
||||
log_target: Cow<'static, str>,
|
||||
log_level: Level,
|
||||
}
|
||||
|
||||
impl Logger {
|
||||
|
@ -102,6 +103,7 @@ impl Logger {
|
|||
exclude: HashSet::new(),
|
||||
exclude_regex: Vec::new(),
|
||||
log_target: Cow::Borrowed(module_path!()),
|
||||
log_level: Level::Info,
|
||||
}))
|
||||
}
|
||||
|
||||
|
@ -139,6 +141,23 @@ impl Logger {
|
|||
self
|
||||
}
|
||||
|
||||
/// Sets the log level to `level`.
|
||||
///
|
||||
/// By default, the log level is `Level::Info`.
|
||||
///
|
||||
/// # Examples
|
||||
/// Using `.log_level(Level::Debug)` 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 DEBUG actix_web::middleware::logger] 127.0.0.1 "GET / HTTP/1.1" 200 88 "-" "dmc/1.0" 0.001985
|
||||
/// ^^^^^^
|
||||
/// ```
|
||||
pub fn log_level(mut self, level: log::Level) -> Self {
|
||||
let inner = Rc::get_mut(&mut self.0).unwrap();
|
||||
inner.log_level = level;
|
||||
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`.
|
||||
|
@ -242,6 +261,7 @@ impl Default for Logger {
|
|||
exclude: HashSet::new(),
|
||||
exclude_regex: Vec::new(),
|
||||
log_target: Cow::Borrowed(module_path!()),
|
||||
log_level: Level::Info,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
@ -312,6 +332,7 @@ where
|
|||
format: None,
|
||||
time: OffsetDateTime::now_utc(),
|
||||
log_target: Cow::Borrowed(""),
|
||||
log_level: self.inner.log_level,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
} else {
|
||||
|
@ -327,6 +348,7 @@ where
|
|||
format: Some(format),
|
||||
time: now,
|
||||
log_target: self.inner.log_target.clone(),
|
||||
log_level: self.inner.log_level,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
@ -344,6 +366,7 @@ pin_project! {
|
|||
time: OffsetDateTime,
|
||||
format: Option<Format>,
|
||||
log_target: Cow<'static, str>,
|
||||
log_level: Level,
|
||||
_phantom: PhantomData<B>,
|
||||
}
|
||||
}
|
||||
|
@ -390,6 +413,7 @@ where
|
|||
let time = *this.time;
|
||||
let format = this.format.take();
|
||||
let log_target = this.log_target.clone();
|
||||
let log_level = this.log_level.clone();
|
||||
|
||||
Poll::Ready(Ok(res.map_body(move |_, body| StreamLog {
|
||||
body,
|
||||
|
@ -397,6 +421,7 @@ where
|
|||
format,
|
||||
size: 0,
|
||||
log_target,
|
||||
log_level,
|
||||
})))
|
||||
}
|
||||
}
|
||||
|
@ -409,6 +434,7 @@ pin_project! {
|
|||
size: usize,
|
||||
time: OffsetDateTime,
|
||||
log_target: Cow<'static, str>,
|
||||
log_level: Level
|
||||
}
|
||||
|
||||
impl<B> PinnedDrop for StreamLog<B> {
|
||||
|
@ -421,8 +447,9 @@ pin_project! {
|
|||
Ok(())
|
||||
};
|
||||
|
||||
log::info!(
|
||||
log::log!(
|
||||
target: this.log_target.as_ref(),
|
||||
this.log_level,
|
||||
"{}", FormatDisplay(&render)
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue