0

I have multiple php applications and when some php errors come up it's logged into Apache log file.

I need to have a detector of when php errors occur inside a script. So when it detects a new php error, the message of the error could be put in a var or saved in database.

ihtus
  • 2,673
  • 13
  • 40
  • 58
  • 1
    Have a look at Exceptions: http://php.net/manual/en/language.exceptions.php and the set-error-handler function in PHP. – Duikboot Oct 28 '13 at 12:48
  • 3
    also http://www.php.net/manual/en/function.set-error-handler.php – Steven V Oct 28 '13 at 12:49
  • What is your problem? Be specific. There are plenty of manual pages which tell you how to setup proper error logging – Alma Do Oct 28 '13 at 12:49

3 Answers3

2

You can use the PHP's output buffer to store all the output within a certain block of code into a variable. Here is an example:

ob_start();
// php code goes here
try{
   // Some code
}catch(Exception $e){
    echo $e->getMessage();
}
$all_output = ob_get_clean();

// Insert the $all_output string into database or log it into a file

In this case, all output such as warnings and anything else that is being echoed between ob_start and ob_get_clean will be stored in $all_output variable. Hope that helps :)

Ayman Farhat
  • 2,010
  • 3
  • 17
  • 16
  • Thanks. Is there another way to write this code? Something like set_exception_handler('yourExceptionHandler'); function yourExceptionHandler($exception) {...} ? – ihtus Oct 28 '13 at 12:58
  • 1
    @ihtus [RTFM](http://php.net/manual/en/function.set-exception-handler.php) – Leri Oct 28 '13 at 13:01
  • `set_exception_handler` only tells PHP what to do about *uncaught* exceptions. If the handler does run, PHP is about to die. This makes it unsuitable if the error can be recovered from and you just want to be notified of its occurrence. – cHao Oct 28 '13 at 13:10
1

You can catch the shutdown errors and use it to insert in database:

register_shutdown_function('shutdownFunction');

function shutDownFunction() { 
    $error = error_get_last();
    if ($error['type'] == 1) {
        //do whatever
    } 
}
Surt
  • 581
  • 1
  • 5
  • 18
0

This essentially boils down to two kinds of errors. Syntax/parser errors, and runtime/logic errors.

For the syntax/parser errors, you're probably out of luck. In those cases the script itself can't execute, so by definition the script can't respond to the errors. The best defense against this is testing (preferably automated). One thing you can do is set your error logging to be a database instead of a file. But the script itself still can't respond to the error.

For runtime/logic errors, this is where exception handling comes into practice. There's a lot that can go into it, but the overall structure is simple enough:

try {
    // perform some operation
} catch (Exception $e) {
    // an error occurred, the details of which are in $e
    // meaningfully respond to it
}
Community
  • 1
  • 1
David
  • 208,112
  • 36
  • 198
  • 279
  • Exceptions actually interfere with logging, in my experience. Logging them would require either catching all errors in one place (making even the smallest error fatal), or repeating "log this exception" everywhere. – cHao Oct 28 '13 at 12:59