0

I have a script that will run on server daily to download data from a resource with no HTML output. By default PHP will output my script errors to php_error.log and to the output window in NetBeans.

I have created a user error handler:

// user defined error handling function
function userErrorHandler($errno, $errmsg, $filename, $linenum, $vars) 
{
    error_log($errmsg, 3, "logs/aem.log");     
    switch ($errno) {
    case E_ERROR:
        exit("Error grave $errmsg at $filename:$linenum");
        break;
    case E_USER_ERROR:
        exit("Error grave $errmsg at $filename:$linenum");
        break;
    }
}

error_reporting(0);
set_error_handler('userErrorHandler');

I include this file in my main script and everything goes fine.

While I develop the app I would like to continue seeing the error messages on Output screen in Netbeans as well as keeping the error log files (as the default handler does). I have tried changing the value of error_reporting or adding additional error_log functions trying the following values:

error_reporting(E_ALL | E_STRICT);
error_reporting(-1);
error_log($errmsg, 0);`

But I never get the error on the output unless I remove include('mycustomhandler'); from the file.

How can I emulate the behavior of the standard error handler?

1 Answer 1

2

You should try to use output to stderr (I guess netbeans parses it). For example:

fprintf( STDERR, "Normal error message %s\n", "With nested info");

For logging errors you may use error_log() (maybe will handle output to netbeans too) or try to parse error_log from php.ini (ini_get()).

However I guess php uses internaly syslog(). So your error handler should look like:

// Logging 
function userErrorHandler($errno, $errmsg, $filename, $linenum, $vars){
  $logLevel = 0;
  $label = '';
  $exit = false;

   switch( $errno){
     case E_ERROR:
     case E_USER_ERROR:
       $label = 'Critical error';
       $exit = true;
       $logLevel = LOG_ERR;
     break;

     // ...
   }

   $message = "$label: on line ... file, error string...";
   if( $logLevel){
     syslog( $logLevel, $message);
   }

   if( !ini_get( 'display_error') && $exit){
     die( 'Fatal error occured.');
   }

   if( $label){
      echo $message; // You're responsible for how error is displayed
   }

   if( $exit){
     die();
   }
}
error_reporting( E_ALL);

Ask questions in comments if I didn't hit what you were asking

Sign up to request clarification or add additional context in comments.

2 Comments

Nice ideas in your handler. I will not use syslog, since the main idea behind using my custom handler is to be able to log to a file specific to the app. fprintf() also prints to Netbeans output so I can add that to the handler.
@DavidCasillas so netbeans parses output from STDERR, good to be sure. And those were just ideas, I hope at least some helped :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.