1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-11 01:39:33 +00:00

feat: change log level based on response status code

This commit is contained in:
fawn 2023-08-20 16:46:45 +03:00
parent d8df60bf4c
commit 7d659229f6
No known key found for this signature in database
GPG key ID: 1F1D882E2C3A12D1

View file

@ -16,7 +16,7 @@ use actix_service::{Service, Transform};
use actix_utils::future::{ready, Ready}; use actix_utils::future::{ready, Ready};
use bytes::Bytes; use bytes::Bytes;
use futures_core::ready; use futures_core::ready;
use log::{debug, warn}; use log::{debug, warn, Level};
use pin_project_lite::pin_project; use pin_project_lite::pin_project;
use regex::{Regex, RegexSet}; use regex::{Regex, RegexSet};
use time::{format_description::well_known::Rfc3339, OffsetDateTime}; use time::{format_description::well_known::Rfc3339, OffsetDateTime};
@ -363,6 +363,12 @@ where
debug!("Error in response: {:?}", error); debug!("Error in response: {:?}", error);
} }
let level = match res.response().status().as_u16() {
300..=499 => Level::Warn,
500..=599 => Level::Error,
_ => Level::Info,
};
let res = if let Some(ref mut format) = this.format { let res = if let Some(ref mut format) = this.format {
// to avoid polluting all the Logger types with the body parameter we swap the body // to avoid polluting all the Logger types with the body parameter we swap the body
// out temporarily since it's not usable in custom response functions anyway // out temporarily since it's not usable in custom response functions anyway
@ -393,6 +399,7 @@ where
format, format,
size: 0, size: 0,
log_target, log_target,
level,
}))) })))
} }
} }
@ -405,6 +412,7 @@ pin_project! {
size: usize, size: usize,
time: OffsetDateTime, time: OffsetDateTime,
log_target: Cow<'static, str>, log_target: Cow<'static, str>,
level: Level,
} }
impl<B> PinnedDrop for StreamLog<B> { impl<B> PinnedDrop for StreamLog<B> {
@ -417,8 +425,9 @@ pin_project! {
Ok(()) Ok(())
}; };
log::info!( log::log!(
target: this.log_target.as_ref(), target: this.log_target.as_ref(),
this.level,
"{}", FormatDisplay(&render) "{}", FormatDisplay(&render)
); );
} }