pub struct KeyFilter { /* private fields */ }Expand description
KeyFilter is inherited from LogFilter, with one additional key into log format.
The name of the key can be customized.
Example for an http service, api handling log will have a field req_id.
When you received error from one of the request,
you can grep all the relevant log with that req_id.
use captains_log::{*, filter::KeyFilter};
fn debug_format_req_id_f(r: FormatRecord) -> String {
let time = r.time();
let level = r.level();
let file = r.file();
let line = r.line();
let msg = r.msg();
let req_id = r.key("req_id");
format!("[{time}][{level}][{file}:{line}] {msg}{req_id}\n").to_string()
}
let builder = recipe::raw_file_logger_custom(
"/tmp/log_filter.log", log::Level::Debug,
recipe::DEFAULT_TIME, debug_format_req_id_f)
.build().expect("setup log");
let logger = KeyFilter::new("req_id", format!("{:016x}", 123).to_string());
info!("API service started");
logger_debug!(logger, "Req / received");
logger_debug!(logger, "header xxx");
logger_info!(logger, "Req / 200 complete");The log will be:
[2025-06-11 14:33:08.089090][DEBUG][request.rs:67] API service started
[2025-06-11 14:33:10.099092][DEBUG][request.rs:67] Req / received (000000000000007b)
[2025-06-11 14:33:10.099232][WARN][request.rs:68] header xxx (000000000000007b)
[2025-06-11 14:33:11.009092][DEBUG][request.rs:67] Req / 200 complete (000000000000007b)Implementations§
Trait Implementations§
Source§impl Filter for KeyFilter
impl Filter for KeyFilter
Source§fn is_enabled(&self, level: Level) -> bool
fn is_enabled(&self, level: Level) -> bool
whether a log level is enable
Auto Trait Implementations§
impl !Freeze for KeyFilter
impl RefUnwindSafe for KeyFilter
impl Send for KeyFilter
impl Sync for KeyFilter
impl Unpin for KeyFilter
impl UnwindSafe for KeyFilter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more