Skip to content

Commit 9bbb541

Browse files
Implements log_level for Logger middleware (#3605)
* implements log level for Logger * docs: update changelog --------- Co-authored-by: Rob Ede <[email protected]>
1 parent 65f254d commit 9bbb541

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

actix-web/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- Add `Logger::log_level()` method.
56
- Add `HttpServer::shutdown_signal()` method.
67
- Mark `HttpServer` as `#[must_use]`.
78
- Re-export `mime` dependency.

actix-web/src/middleware/logger.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use actix_service::{Service, Transform};
1616
use actix_utils::future::{ready, Ready};
1717
use bytes::Bytes;
1818
use futures_core::ready;
19-
use log::{debug, warn};
19+
use log::{debug, warn, Level};
2020
use pin_project_lite::pin_project;
2121
#[cfg(feature = "unicode")]
2222
use regex::Regex;
@@ -92,6 +92,7 @@ struct Inner {
9292
exclude: HashSet<String>,
9393
exclude_regex: Vec<Regex>,
9494
log_target: Cow<'static, str>,
95+
log_level: Level,
9596
}
9697

9798
impl Logger {
@@ -102,6 +103,7 @@ impl Logger {
102103
exclude: HashSet::new(),
103104
exclude_regex: Vec::new(),
104105
log_target: Cow::Borrowed(module_path!()),
106+
log_level: Level::Info,
105107
}))
106108
}
107109

@@ -139,6 +141,23 @@ impl Logger {
139141
self
140142
}
141143

144+
/// Sets the log level to `level`.
145+
///
146+
/// By default, the log level is `Level::Info`.
147+
///
148+
/// # Examples
149+
/// Using `.log_level(Level::Debug)` would have this effect on request logs:
150+
/// ```diff
151+
/// - [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
152+
/// + [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
153+
/// ^^^^^^
154+
/// ```
155+
pub fn log_level(mut self, level: log::Level) -> Self {
156+
let inner = Rc::get_mut(&mut self.0).unwrap();
157+
inner.log_level = level;
158+
self
159+
}
160+
142161
/// Register a function that receives a ServiceRequest and returns a String for use in the
143162
/// log line. The label passed as the first argument should match a replacement substring in
144163
/// the logger format like `%{label}xi`.
@@ -242,6 +261,7 @@ impl Default for Logger {
242261
exclude: HashSet::new(),
243262
exclude_regex: Vec::new(),
244263
log_target: Cow::Borrowed(module_path!()),
264+
log_level: Level::Info,
245265
}))
246266
}
247267
}
@@ -312,6 +332,7 @@ where
312332
format: None,
313333
time: OffsetDateTime::now_utc(),
314334
log_target: Cow::Borrowed(""),
335+
log_level: self.inner.log_level,
315336
_phantom: PhantomData,
316337
}
317338
} else {
@@ -327,6 +348,7 @@ where
327348
format: Some(format),
328349
time: now,
329350
log_target: self.inner.log_target.clone(),
351+
log_level: self.inner.log_level,
330352
_phantom: PhantomData,
331353
}
332354
}
@@ -344,6 +366,7 @@ pin_project! {
344366
time: OffsetDateTime,
345367
format: Option<Format>,
346368
log_target: Cow<'static, str>,
369+
log_level: Level,
347370
_phantom: PhantomData<B>,
348371
}
349372
}
@@ -390,13 +413,15 @@ where
390413
let time = *this.time;
391414
let format = this.format.take();
392415
let log_target = this.log_target.clone();
416+
let log_level = *this.log_level;
393417

394418
Poll::Ready(Ok(res.map_body(move |_, body| StreamLog {
395419
body,
396420
time,
397421
format,
398422
size: 0,
399423
log_target,
424+
log_level,
400425
})))
401426
}
402427
}
@@ -409,6 +434,7 @@ pin_project! {
409434
size: usize,
410435
time: OffsetDateTime,
411436
log_target: Cow<'static, str>,
437+
log_level: Level
412438
}
413439

414440
impl<B> PinnedDrop for StreamLog<B> {
@@ -421,8 +447,9 @@ pin_project! {
421447
Ok(())
422448
};
423449

424-
log::info!(
450+
log::log!(
425451
target: this.log_target.as_ref(),
452+
this.log_level,
426453
"{}", FormatDisplay(&render)
427454
);
428455
}

0 commit comments

Comments
 (0)