如何将php错误消息读取到字符串变量中


how to read a php error message into a string variable

我正在做一个小程序,当出现错误时,它需要读取错误信息。

例如,一个操作触发了一个错误,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')知道可以打印错误消息。但我需要阅读它,以便格式化它

我可以通过什么方式做到这一点?欢迎回答。:)

使用此选项:http://www.php.net/manual/en/function.error-get-last.php,对于处理错误,我建议您只使用自定义异常和错误处理程序

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

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

实际上,访问致命错误的任何错误信息的唯一方法是使用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']
       */
   }
});