Logging PHP Errors to a Database Instead of error_log
Is it feasible to configure PHP to log errors directly to a MySQL database rather than the default error_log file? Although implementing a custom error handler is a viable approach, this would necessitate significant code modifications. Is there a more global solution?
Answer:
Unfortunately, it is not possible to accomplish this without creating a custom error handler. However, this custom error handler would effectively serve as the desired "one global change."
The modified example from the PHP manual demonstrates how this can be achieved:
function myErrorHandler($errno, $errstr, $errfile, $errline) { // Establish a connection to the MySQL database here mysql_query("INSERT INTO error_log (number, string, file, line) ". "VALUES ....."); /* Suppress PHP internal error handler from executing */ return true; }
To activate the custom error handler:
// Assign the custom error handler $old_error_handler = set_error_handler("myErrorHandler");
The above is the detailed content of Can PHP Log Errors Directly to a MySQL Database?. For more information, please follow other related articles on the PHP Chinese website!