8

Is it possible to make all PHP errors be written to MySQL instead of to the standard error_log file. I guess this would be possible if i wrote my own error handler from scratch but i have a lot of legacy code in place and ideally i would just make 1 global change and that would be it. Can this be done?

robjmills
  • 18,129
  • 15
  • 73
  • 119
  • I'm 99% positive you can't just put something in PHP.ini and have it happen. That being said writing a custom error handler would take you about 5 minutes. – Erik May 26 '10 at 08:09

2 Answers2

21

I don't think it can be done without building an own error handler, but technically, that is the one global change you're looking for.

Modified example from the manual:

function myErrorHandler($errno, $errstr, $errfile, $errline)
{
     // you'd have to import or set up the connection here 
     mysql_query("INSERT INTO error_log (number, string, file, line) ".
                 "VALUES .....");         

    /* Don't execute PHP internal error handler */
    return true;
}

then

// set to the user defined error handler
$old_error_handler = set_error_handler("myErrorHandler");
Pekka
  • 431,103
  • 135
  • 960
  • 1,075
2

It looks like the php function set_error_handler might do what you're looking for. In the first example you can add some mysql inserts to write your errors to a database.

http://www.php.net/manual/en/function.set-error-handler.php

Calvin
  • 8,417
  • 7
  • 41
  • 50