Log - Target - Email and recurring emails with the same error

I have log - target:

'error' => [
    'class' => 'yii\log\EmailTarget',
    'levels' => ['error', 'warning'],
    'except' => ['yii\web\HttpException:404'],
    'message' => [
        'from' => ['XXXXXXX'],
        'to' => ['XXXXXXX'],
        'subject' => 'XXXXXX - error',

It will send me an email whenever an application error/warning occurs. It’s OK.

The problem is a “long duration” error (for example database down, but it could be anything else).
It sends an email for every visit the website.
With high traffic = hundreds of emails with the same error during one minute.

How prevent recurring emails with the same error?

For production it’s much better to use something like https://sentry.io/welcome/ (they have OpenSource version) or https://rollbar.com/. They’s both recording each error instance with full context, getting you a list of the most repeating errors and sending summary emails.

I prefer a simple solution over implementing another error management system

Simple solution is to take existing solution but if you need to re-invent your own then:

  1. You need to generalize error i.e. strip parameters out of it.
  2. You need to store hash of this stripped down error when sending email.
  3. When deciding if next one should be sent, search for its hash in database. If it’s there then do not send an email.