pub struct LogFilterKV { /* private fields */ }Expand description
LogFilterKV 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::LogFilterKV};
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 = LogFilterKV::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 Clone for LogFilterKV
impl Clone for LogFilterKV
Source§fn clone(&self) -> LogFilterKV
fn clone(&self) -> LogFilterKV
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl !Freeze for LogFilterKV
impl RefUnwindSafe for LogFilterKV
impl Send for LogFilterKV
impl Sync for LogFilterKV
impl Unpin for LogFilterKV
impl UnwindSafe for LogFilterKV
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