Welcome Guest, Not a member yet? Register   Sign In
How can I have PHP Warnings emailed to me?
#1

[eluser]Ian Cook[/eluser]
I'd like to receive error logs via email. For example, if a Warning-level error message should occur, I'd like to get an email about it.

How can I get that working in CI ?

Thanks,

Ian
#2

[eluser]Randy Casburn[/eluser]
Hi thinkspill,

Write a class named MY_Exceptions and put in in application/libraries. In that file you'll extend the Exceptions class and create your e-mail method there.

You have a couple of options on capturing the error. You could catch the severity (Warning-level) by overriding the log_exception() method but that presumes the error will be logged. Or you could capture the error by overriding the show_php_error() method but this is unlikely to work reliably because you'll have your error reporting tuned down so low it may not trigger your e-mail.

So the log_exception() method capture seems to be the best choice. Test for $severity and branch off to your new email_exception() method in your MY_exception() class.

Hope this helps,

Randy
#3

[eluser]dexter21[/eluser]
Hi, i have following /system/application/errors/error_php.php , when i have an error of code this send to me by email. Hope this helps,


Code:
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: &lt;?php echo $severity; ?&gt;</p>
<p>Message:  &lt;?php echo $message; ?&gt;</p>
<p>Filename: &lt;?php echo $filepath; ?&gt;</p>
<p>Line Number: &lt;?php echo $line; ?&gt;</p>
<br />
El error se envi&oacute; correctamente al administrador
</div>

&lt;?
$CI =& get_instance();


$cad_error='
            <div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
                <h4>A PHP Error was encountered</h4>

            <p>Severity:'.$severity.'</p>
            <p>Message: '.$message.'</p>
            <p>Filename: '.$filepath.'</p>
            <p>Line Number: '.$line.'</p>';

            $cad_error .='HORA:'.date("d/m/Y H:i:s")."<br>";
            $cad_error .='USER AGENT: '.$_SERVER["HTTP_USER_AGENT"]."<br>";
            $cad_error .='IP REMOTA: '.$_SERVER["REMOTE_ADDR"]."<br>";
            $cad_error .="<hr>";
            
            foreach($_SERVER as $campo=>$valor)
            {
                $cad_error .=$campo." : ".$valor."<br>";
            }
            
            
            $cad_error .='</div>';
        
            $to = '[email protected]';
            $subject = 'ERROR in php code';
            $message = $cad_error;
            $headers =     'Mime-Version: 1.0\n' . "\n" .
                        'Content-type: text/html; charset=iso-8859-1'. "\n" .
                        'Reply-To: [email protected]' . "\n" .
                        'From: ERROR REPORT <[email protected]>';
            $mail_sent = @mail( $to, $subject, $message, $headers );
            
?&gt;
#4

[eluser]barbazul[/eluser]
@dexter21: Your solution sure works but it messes up the MVC since the error_php.php is mainly a "View " it's no place to put the email sending

I'd go with Randy's solution

Here's some example code (might need adjustments):

File: /system/application/libraries/MY_Exception.php
Code:
class MY_Exception extends Exception {

  function log_exception($severity, $message, $filepath, $line) { // You need the same header

    if ($severity == E_WARNING) { // There are better ways to validate this
      call_email_method_or_whatever();
    }
    
    parent::log_exception($severity, $message, $filepath, $line);
  }

}
#5

[eluser]bd3521[/eluser]
[quote author="barbazul" date="1225828071"]@dexter21: Your solution sure works but it messes up the MVC since the error_php.php is mainly a "View " it's no place to put the email sending

I'd go with Randy's solution

Here's some example code (might need adjustments):

File: /system/application/libraries/MY_Exception.php
Code:
class MY_Exception extends Exception {

  function log_exception($severity, $message, $filepath, $line) { // You need the same header

    if ($severity == E_WARNING) { // There are better ways to validate this
      call_email_method_or_whatever();
    }
    
    parent::log_exception($severity, $message, $filepath, $line);
  }

}
[/quote]

This is excellent, thank you. Any way to prevent the same error being emailed multiple times?
#6

[eluser]Randy Casburn[/eluser]
Sure, if you implemented it as barbazul recommended above, then the exact error has been logged by the parent::log_exception() method. You could read the log and look for duplicates. Since I'm presuming you've already warned away your users or provided them the necessary "get well" messages, performance shouldn't be an issue here. I'm making some leaps of faith of course.


That simple,

Randy




Theme © iAndrew 2016 - Forum software by © MyBB