Skip to content

Commit

Permalink
Do not send email too frequently
Browse files Browse the repository at this point in the history
  • Loading branch information
majecty committed Nov 25, 2020
1 parent 7042793 commit 6e67d80
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion util/logger/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

use std::env;
use std::thread;
use std::time::{Duration, Instant};
use time;

use atty;
use colored::Colorize;
use env_logger::filter::{Builder as FilterBuilder, Filter};
use log::{Level, LevelFilter, Log, Metadata, Record};
use parking_lot::Mutex;

use crate::{email::EmailAlarm, structured_logger, SLOGGER};

Expand All @@ -42,6 +44,7 @@ pub struct Logger {
filter: Filter,
stderr_is_tty: bool,
email_alarm: Option<EmailAlarm>,
last_email_sent: Mutex<Option<Instant>>,
}

impl Logger {
Expand All @@ -60,6 +63,7 @@ impl Logger {
filter: builder.build(),
stderr_is_tty,
email_alarm,
last_email_sent: Mutex::new(None),
}
}

Expand Down Expand Up @@ -110,7 +114,15 @@ impl Log for Logger {

if log_level == Level::Error {
if let Some(email_alarm) = &self.email_alarm {
email_alarm.send(&format!("{} {} {}", thread_name, log_target, log_message))
let mut last_email_sent = self.last_email_sent.lock();
let sent_recently = match *last_email_sent {
Some(last_sent) => last_sent.elapsed() < Duration::from_secs(300),
None => false,
};
if !sent_recently {
email_alarm.send(&format!("{} {} {}", thread_name, log_target, log_message));
*last_email_sent = Some(Instant::now());
}
}
}
}
Expand Down

0 comments on commit 6e67d80

Please sign in to comment.