Skip to content

Do not send email too frequently #1995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion util/logger/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ 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 std::env;
use std::thread;
use std::time::{Duration, Instant};
use time;

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

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

Expand Down Expand Up @@ -108,7 +112,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