fix mail level filter, skip mail double mail lines

This commit is contained in:
jb-alvarado 2024-08-18 18:14:21 +02:00
parent 0a53eb3f80
commit 06c529585d
2 changed files with 25 additions and 13 deletions

View File

@ -18,6 +18,7 @@ use lettre::{
}; };
use log::{kv::Value, *}; use log::{kv::Value, *};
use paris::formatter::colorize_string; use paris::formatter::colorize_string;
use regex::Regex;
use super::ARGS; use super::ARGS;
@ -133,11 +134,15 @@ impl LogWriter for MultiFileLogger {
pub struct LogMailer { pub struct LogMailer {
pub mail_queues: Arc<Mutex<Vec<Arc<Mutex<MailQueue>>>>>, pub mail_queues: Arc<Mutex<Vec<Arc<Mutex<MailQueue>>>>>,
raw_lines: Arc<Mutex<Vec<String>>>,
} }
impl LogMailer { impl LogMailer {
pub fn new(mail_queues: Arc<Mutex<Vec<Arc<Mutex<MailQueue>>>>>) -> Self { pub fn new(mail_queues: Arc<Mutex<Vec<Arc<Mutex<MailQueue>>>>>) -> Self {
Self { mail_queues } Self {
mail_queues,
raw_lines: Arc::new(Mutex::new(vec![])),
}
} }
} }
@ -164,16 +169,26 @@ impl LogWriter for LogMailer {
poisoned.into_inner() poisoned.into_inner()
}); });
if q_lock.id == id && q_lock.level_eq(record.level()) { let msg = strip_tags(&record.args().to_string());
let mut raw_lines = self.raw_lines.lock().unwrap();
if q_lock.id == id && q_lock.level_eq(record.level()) && !raw_lines.contains(&msg) {
q_lock.push(format!( q_lock.push(format!(
"[{}] [{:>5}] {}", "[{}] [{:>5}] {}",
now.now().format("%Y-%m-%d %H:%M:%S"), now.now().format("%Y-%m-%d %H:%M:%S"),
record.level(), record.level(),
record.args() msg.clone()
)); ));
raw_lines.push(msg);
break; break;
} }
if raw_lines.len() > 1000 {
let last = raw_lines.pop().unwrap();
raw_lines.clear();
raw_lines.push(last);
}
} }
Ok(()) Ok(())
@ -200,15 +215,7 @@ impl MailQueue {
} }
pub fn level_eq(&self, level: Level) -> bool { pub fn level_eq(&self, level: Level) -> bool {
match level { level <= self.config.mail_level
Level::Error => self.config.mail_level == Level::Error,
Level::Warn => matches!(self.config.mail_level, Level::Warn | Level::Error),
Level::Info => matches!(
self.config.mail_level,
Level::Info | Level::Warn | Level::Error
),
_ => false,
}
} }
pub fn update(&mut self, config: Mail) { pub fn update(&mut self, config: Mail) {
@ -232,6 +239,11 @@ impl MailQueue {
} }
} }
fn strip_tags(input: &str) -> String {
let re = Regex::new(r"<[^>]*>").unwrap();
re.replace_all(input, "").to_string()
}
fn console_formatter(w: &mut dyn Write, _now: &mut DeferredNow, record: &Record) -> io::Result<()> { fn console_formatter(w: &mut dyn Write, _now: &mut DeferredNow, record: &Record) -> io::Result<()> {
match record.level() { match record.level() {
Level::Debug => write!( Level::Debug => write!(

@ -1 +1 @@
Subproject commit c7a4fc31516fb4735076f6e9f15cc2b273b58a62 Subproject commit 2045e47ab5e9f42ff73e03a7c4c604821a73f7ca