Jump to content

PHP custom error logging performance issues


sunwukung

Recommended Posts

Hi, I've been trying to write an error handling class that I can use on sites, that will email me in the event of an error. Problem is, when I profile the application, it's choking on the error_log function. Here's my code (omitting the class:

 

class ErrorHandler
{
private static $instance;
private static $mail;
private function __clone(){}

private function __construct()
    {
    error_reporting( E_ALL | E_STRICT );

    if(!defined('ENV')){
        if($_SERVER['SERVER_ADDR']=='127.0.0.1' || $_SERVER['SERVER_NAME']=='localhost')
            {
            #echo"local environment<br>";
            DEFINE('ENV','LOCAL');
            ini_set('display_errors', 1);
            }
        else
            {
            #echo"live environment";
            DEFINE('ENV','LIVE');
            ini_set('display_errors', 0);
            }
        }
    }

public function setErrorConfig($error_level,$mail='',$mode='production')
    {
    error_reporting($error_level);
    switch($mode)
        {
        case 'development':
        ini_set('display_errors', '1');
        break;

        case 'production':
        ini_set('display_errors', '0');
        if($mail != ''){
            self::$mail = $mail;
            set_error_handler(array('ErrorHandler', 'handleError'));
            }
        break;

        default:
        ini_set('display_errors', '0');
        error_reporting( E_ERROR );
        break;
        }
    }

public function handleError($e_num,$e_msg,$e_file,$e_line,$e_vars)
    {
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: DC_Research Site' . "\r\n";

    $msg = '';
    $msg .= '<html><head></head><body>';
    $msg .= '<STYLE>h2{font-family:verdana;}</STYLE>';
    $msg .= '<h2>Error Description:</h2>';
    $msg .= '<h2>Script:</h2><p>'.$e_file.'</p>';
    $msg .= '<h2>Line:</h2><p>'.$e_line.'</p>';
    $msg .= '<h2>Message:</h2><p>'.$e_msg.'</p>';
    $msg .= '<h2>Variables:</h2><p>'.$e_vars.'</p>';
    $msg .= '</html></body>';

    #mail(self::$mail,'Error Report',$msg,$headers);
    error_log($msg,1,self::$mail,$headers);
    }
}

 

Can you help me figure out what's killing it?

Link to comment
Share on other sites

Sorry - I should add that I've omitted this part of the code for the sake of brevity:

 

public static function getInstance(){
         if( !isset( self::$instance ) ){
           self::$instance = new self();
           }
         return self::$instance;
         }

 

Which allows you to access it's methods.

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.