将所有PHP错误输出到数据库而不是error_log

时间:2022-04-26 22:00:54

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?

是否可以将所有PHP错误写入MySQL而不是标准的error_log文件。我想这是可能的,如果我从头开始编写自己的错误处理程序,但我有很多遗留代码,理想情况下我只会进行1次全局更改,那就是它。可以这样做吗?

2 个解决方案

#1


20  

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");

#2


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.

貌似php函数set_error_handler可能会做你想要的。在第一个示例中,您可以添加一些mysql插入以将错误写入数据库。

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

#1


20  

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");

#2


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.

貌似php函数set_error_handler可能会做你想要的。在第一个示例中,您可以添加一些mysql插入以将错误写入数据库。

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