Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change log level based on response status code #3107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions actix-web/src/middleware/logger.rs
Expand Up @@ -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;
use regex::{Regex, RegexSet};
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
Expand Down Expand Up @@ -363,6 +363,12 @@ where
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 {
// 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
Expand Down Expand Up @@ -393,6 +399,7 @@ where
format,
size: 0,
log_target,
level,
})))
}
}
Expand All @@ -405,6 +412,7 @@ pin_project! {
size: usize,
time: OffsetDateTime,
log_target: Cow<'static, str>,
level: Level,
}

impl<B> PinnedDrop for StreamLog<B> {
Expand All @@ -417,8 +425,9 @@ pin_project! {
Ok(())
};

log::info!(
log::log!(
target: this.log_target.as_ref(),
this.level,
"{}", FormatDisplay(&render)
);
}
Expand Down