Jump to content

When there's an error, sending too many emails to self. How to limit to just one?


Fluoresce

Recommended Posts

I handle MySQL errors like this:

function handle_error($err) {
     throw new Exception($err);
}
try {     
     $con = mysql_connect("", "", "") or handle_error("mysql_connect failed! ");
     mysql_select_db("") or handle_error("mysql_select_db failed! ");
     $sql = "SELECT blah blah blah ...";
     $result = mysql_query($sql, $con) or handle_error("mysql_query failed! ");                   
}
catch(exception $e) {                                           
     // Log error in error_log file.     
     trigger_error($e->getMessage() . mysql_error(), E_USER_WARNING);     
     // Send yourself an email.     
     error_log($e->getMessage() . "Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "example@aol.com", "From: example@yahoo.com");
}
if(isset($result)) {
     blah blah blah ...
}

The problem is, I can receive a huge number of e-mails whenever there's a connection error.

 

How can I limit the number of e-mails that I send myself in the event that my database fails?

 

I really haven't a clue. :confused:

 

The only idea that I had was store the time that I sent the first e-mail in a database and then check the database before sending any other e-mails. But this obviously won't work because MySQL won't be working.

 

Any ideas will be much appreciated.

Link to comment
Share on other sites

Mac_gyver has provided a solid solution that you should consider. But, there is a simple 'hack' to get you a workable solution in the short run.

 

If an error is encountered, first check the modified date of the log file. If it is more than N minutes since it was last modified, send the email. Otherwise do not send the email But, in either case you would still log the error.

 

The result is that once you start getting errors you will get an email. But, you would not get another email from any failures unless there were no errors for the given amount of time.

  • Like 1
Link to comment
Share on other sites

Thank you both. :happy-04:

 

Psycho, is this kind of what you mean? It's supposed to send me an e-mail if more than 30 minutes (1,800 seconds) have elapsed since the previous e-mail.

<?php
    $file = 'error_log';
    $last_modified = filemtime($file);    
    if(time() - $last_modified > 1800) {
        error_log($e->getMessage() . "Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "example@aol.com", "From: example@yahoo.com");
    }
?>

The error log file (which seems to have been automatically created after my first error) doesn't seem to have an extension (such as .txt or whatever). Do I just leave the file name as "error_log"?

Edited by Fluoresce
Link to comment
Share on other sites

Thank you both. :happy-04:

 

Psycho, is this kind of what you mean? It's supposed to send me an e-mail if more than 30 minutes (1,800 seconds) have elapsed since the previous e-mail.

 

The error log file (which seems to have been automatically created after my first error) doesn't seem to have an extension (such as .txt or whatever). Do I just leave the file name as "error_log"?

 

If it is working for you then, yes, that is what I mean.

 

I can't comment on the error log file name. You appear to be using a class for your error logging. You would have to look at the code that creates that file to change it to something else - if that is what you want to do.

  • Like 1
Link to comment
Share on other sites

I can't comment on the error log file name. You appear to be using a class for your error logging. You would have to look at the code that creates that file to change it to something else - if that is what you want to do.

 

I haven't set up any classes. All I know is that there's a file in my folder called error_log. It has no extension such as .txt or .php. And when I check my php.ini file, it says:

; Log errors to specified file.
error_log = error_log;

I'm therefore assuming that the file called error_log was created automatically when my first error occurred.

 

Should I create a specific file myself, such as a .txt file?

 

Should the setup look something like this?

; Log errors to specified file.
error_log = /home/user/public_html/domainfolder/error-log-file.txt;

Examples given online never seem to have a clear path and they all seem to have log files with a .log extension.

Link to comment
Share on other sites

I'm really confused.

 

Can anyone see why the following code logs an error but doesn't send me an e-mail when there's a connection problem?

function handle_error($err) {
     throw new Exception($err);
}
try {     
     $con = mysql_connect("", "", "") or handle_error("mysql_connect failed! ");
     mysql_select_db("") or handle_error("mysql_select_db failed! ");
     $sql = "SELECT blah blah blah ...";
     $result = mysql_query($sql, $con) or handle_error("mysql_query failed! ");                   
}
catch(exception $e) {                                           
     // Log error in error-log.txt.     
     trigger_error($e->getMessage() . mysql_error(), E_USER_WARNING);     
     // Send yourself an email if more than 30 mins (1800 seconds) have passed since previous e-mail.   
     $last_modified = filemtime('error-log.txt');
     $time_elapsed = time() - $last_modified;
     if($time_elapsed > 1800) {
          error_log($e->getMessage() . "Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "example@aol.com", "From: example@yahoo.com");
     }
}

The variables  $last_modified  and $time_elapsed are being set. I know that  much.

 

The problem seems to be the if statement. When I remove it, an e-mail is sent.

 

Can we not have if statements in catch blocks or something?

Edited by Fluoresce
Link to comment
Share on other sites

the time check conditional statement is to send the email. after you get the $last_modified value, you will unconditionally call the error_log() statement to write the error information to the log file. 

if($time_elapsed > 1800) {
 
   // your email code would go here....

}

the error_log() statement has a mode that accepts the filename as a parameter (though using it this way, you might as well use file_put_contents() with the append flag.) this would avoid the need to set any php.ini values. you code would have a variable holding the file name that gets used in both the filemtime() statement and the error_log() statement.

Link to comment
Share on other sites

Thanks, guys.

I can see the problem now. It's quite stupid of me. :facewall:

This is what the script's doing:
 

  1. Whenever there's an error, log it in error-log.txt.
  2. Check when error-log.txt was last modified (which would always be just a split second ago).
  3. Send e-mail only if $last_modified > 1,800 (which it never is).

 

Even if I changed the order so that the e-mail is sent before the error is logged, the script is still flawed because it would not work if errors are constantly occurring. It would send the first e-mail and that's it; it won't send one every 30 minutes.

The only way around it is to check not when error-log.txt was last modified but instead check when I last sent an e-mail.

 

This is my new code—and it works!

// When there's an error, log it in error file.

trigger_error($e->getMessage() . mysql_error(), E_USER_WARNING);

// Also, send yourself an email if more than 30 minutes have passed since previous error email.

$LastEmailSent = (int)file_get_contents('last_email_sent.txt');
$TimeElapsed = time() - $LastEmailSent;
if($TimeElapsed > 1800) {
     error_log($e->getMessage() . "Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "example@aol.com", "From: example@yahoo.com");
     file_put_contents('last_email_sent.txt', time());
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.