Can PHP errors be redirected from error_log to a MySQL database?
By default, PHP errors are written to the error_log file. While it's possible to create a custom error handler, there may be concerns about modifying legacy code.
Custom Error Handler Approach
The recommended solution is to create a custom error handler, which allows complete control over how errors are handled. This is considered a single global change, as it replaces the default error logging behavior.
Here's an example of a custom error handler:
function myErrorHandler($errno, $errstr, $errfile, $errline) { // Import or set up MySQL connection mysql_query("INSERT INTO error_log (number, string, file, line) " . "VALUES ('$errno', '$errstr', '$errfile', '$errline')"); // Prevent PHP's internal error handler from running return true; }
Implementing the Error Handler
To implement the custom error handler, use:
$old_error_handler = set_error_handler("myErrorHandler");
This sets the error handler to your custom function. Now, PHP errors will be logged to the specified MySQL database.
Remember that modifying legacy code may require careful consideration of potential consequences and compatibility issues.
The above is the detailed content of Can PHP Errors Be Logged to a MySQL Database?. For more information, please follow other related articles on the PHP Chinese website!