如何获取错误和异常,并将它们写入日志文件


How can I get errors and exceptions ,and write them to log files?

在我的PHP项目中,我想在产品服务器中获取所有错误和异常。但我如何获取它们并将它们连接到我的日志文件中?

您可以在catch块中执行此操作:

try {
    foo();
} catch (Exception $e) {
    error_log("Caught $e");
}

或者在异常处理程序中:

set_exception_handler(function($exception) {
    error_log($exception);
    error_page("Something went wrong!");
});