php - 如何在 PHP 中记录未捕获的异常?

标签 php exception error-handling error-logging

我已经找到了如何将错误转换为异常,如果它们没有被捕获,我会很好地显示它们,但我不知道如何以有用的方式记录它们。简单地将它们写入文件不会有用,是吗?如果您还不知道导致异常的原因,您会冒险访问数据库吗?

最佳答案

你可以使用 set_error_handler设置自定义异常以记录您的错误。我个人会考虑将它们存储在数据库中,因为默认的异常处理程序的回溯可以提供有关导致它的原因的信息 - 如果数据库处理程序触发了异常,这当然是不可能的。

你也可以使用 error_log记录你的错误。它可以选择消息目的地,包括:

Quoted from error_log

  1. PHP's system logger, using the Operating System's system logging mechanism or a file, depending on what the error_log configuration directive is set to. This is the default option.
  2. Sent by email to the address in the destination parameter. This is the only message type where the fourth parameter, extra_headers is used.
  3. Appended to the file destination . A newline is not automatically added to the end of the message string.

编辑:markdown 有下划线的 noparse 标签吗?

关于php - 如何在 PHP 中记录未捕获的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20322/

相关文章:

php - MySql Regexp 从用户输入中搜索

javascript - JS单页应用程序和基于PHP的登录系统?

javascript - ASP.NET Ajax 错误 : Sys. WebForms.PageRequestManagerParserErrorException

asp.net-mvc - 这是 ASP.NET MVC 中异常处理的正确方法吗

function - 为什么我收到此错误消息 “UnboundLocalError: local variable ' sigma_opt在分配前被引用”

php - 在OO PHP中捕获异常

python - 处理 python 中的错误

php - 选择每种类型的多个项目

php - wordwrap()... 只打破特定长度的单词

javascript - CreateGlobalJavascriptObject 问题