我正在做一个小程序,需要在发生错误时读取错误消息。
例如,一个操作触发了一个错误,php日志将其记录为:
PHP Fatal error: Smarty error: [in /mnt/n2my_web/templates/ja_JP/mail/reservation_create.t.txt line 16]: syntax error: mismatched tag {/if}. (Smarty_Compiler.class.php, line 2338) in /mnt/n2my_web/lib/Smarty/Smarty.class.php on line 1092
我知道通过设置 ini_set('display_errors','1'); 可以打印错误信息。但我需要阅读它,以便格式化它。
我可以通过哪种方式实现这一目标?任何答案都表示赞赏。 :)
答案 0 :(得分:2)
使用此:http://www.php.net/manual/en/function.error-get-last.php来处理错误,我建议您只使用自定义异常和错误处理程序
http://www.php.net/manual/en/function.set-exception-handler.php
答案 1 :(得分:0)
真正唯一能够访问致命错误的任何错误信息的方法是使用register_shutdown_function()
来尝试捕获这些错误并在脚本终止之前使用它们。这不是100%可靠。
register_shutdown_function(function() {
$last_error = error_get_last();
if(!is_null($last_error) && $last_error['type'] === E_ERROR) {
/*
Do something with $last_error info.
$last_error will contain array with keys as follows:
['type']['message']['file']['line']
*/
}
});