pub fn set_arg_formatter(
forrmatter: Box<dyn ArgFormatter + Send + Sync>,
) -> Result<(), ()>
Available on crate feature
custom-arg-formatter
only.Expand description
Use a custom formater to format the args (the actual message) of the record . This function can only be called once and return an Error if called a second time.
This example remove the private user ipv4 loggeg by hickory
from the log, if the loglevel is below Debug.
use env_logger::fmt::Formatter;
use log::{Level, Record};
use once_cell::sync::Lazy;
use regex::Regex;
use std::{io, io::Write};
static REGEX: Lazy<Regex> = Lazy::new(|| {
// https://ihateregex.io/expr/ip/
Regex::new(r"(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}")
.unwrap()
});
fn arg_format(buf: &mut Formatter, record: &Record<'_>) -> io::Result<()> {
if let Some(mod_path) = record.module_path() {
if log::max_level() < Level::Debug && mod_path.starts_with("hickory") {
let message = format!("{}", record.args());
let message = REGEX.replace_all(&message, "RESTRAINED");
return writeln!(buf, "{}", message);
}
};
writeln!(buf, "{}", record.args())
}
my_env_logger_style::set_arg_formatter(Box::new(arg_format)).unwrap();