如何捕获所有 PHP 错误?

发布于 2024-10-09 00:47:11 字数 126 浏览 0 评论 0原文

我需要一个解决方案来捕获所有 PHP 致命错误、异常、警告等并进行回调。

我想向用户显示错误的友好版本并记录该错误。

我正在考虑每天使用一个文本文件来记录错误。

有什么建议或 PHP 类(库)吗?

I need a solution to catch all PHP fatal errors, exceptions, warnings, etc. and have a callback.

I want to display a friendly version of the error to the user and log that error.

I'm thinking about using a text file per day for logging error.

Any suggestion or PHP class (library)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

听不够的曲调 2024-10-16 00:47:11

从 PHP 8 开始,捕获所有异常的最佳方法是捕获 Throwable< /a> 接口“是可以通过 throw 语句抛出的任何对象的基本接口”。所以你的代码看起来像这样。

try {
    # code...
} catch (\Throwable $th) {
    # code...
}

As of PHP 8 the best way to catch any and all Exceptions is to catch the Throwable interface which "is the base interface for any object that can be thrown via a throw statement". So your code would look something like this.

try {
    # code...
} catch (\Throwable $th) {
    # code...
}
请远离我 2024-10-16 00:47:11

这使得几乎所有错误都成为 ErrorException 的可捕获实例:

set_error_handler(function($errno, $errstr, $errfile, $errline ){
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
});

在可能给出错误的代码之前使用它,例如在 php 文件的最顶部或包含

Limits< 的 公共标头中的实例/em>: 无法使用用户定义的函数处理严重错误(PHP 引擎、服务器、语法):E_ERRORE_PARSEE_CORE_ERRORE_CORE_WARNINGE_COMPILE_ERRORE_COMPILE_WARNING 以及文件中引发的大部分 E_STRICT 内容set_error_handler()被调用妥协了。

但是,如果语法正确并且服务器没有损坏,则不应出现这些错误。

如果需要,您可以使用 register_shutdown_function()error_get_last() 解决此问题

This makes almost all errors become catchable instance of ErrorException:

set_error_handler(function($errno, $errstr, $errfile, $errline ){
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
});

use it before of the code that can give errors, for instances at the very top of your php file or in a common header included

Limits: Severest errors (PHP engine, server, syntax) cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called compromise it.

But, if syntax is correct and server don't broke, these errors should not appear.

If needed, you could workaround it with the register_shutdown_function() and error_get_last()

且行且努力 2024-10-16 00:47:11

尝试启动此网页,您应该看到“消息:除以零”。

// Set Error Handler

set_error_handler (
    function($errno, $errstr, $errfile, $errline) {
        throw new ErrorException($errstr, $errno, 0, $errfile, $errline);     
    }
);

// Trigger an exception in a try block

try {
    $a = 3/0;
    echo $a;
}
catch(Exception $e) {
    echo 'Message: ' .$e->getMessage();
}

Try launch this web page, you should see "Message: Division by Zero".

// Set Error Handler

set_error_handler (
    function($errno, $errstr, $errfile, $errline) {
        throw new ErrorException($errstr, $errno, 0, $errfile, $errline);     
    }
);

// Trigger an exception in a try block

try {
    $a = 3/0;
    echo $a;
}
catch(Exception $e) {
    echo 'Message: ' .$e->getMessage();
}
空城缀染半城烟沙 2024-10-16 00:47:11

我非常喜欢 kohana 框架的错误处理。不过,您必须做一些工作才能将其拉出来。

http://kohanaframework.org/

它将允许您将错误记录到文件并向收件人发送电子邮件。它还使您能够重定向到友好的错误页面。

I quite like the error handling from the kohana framework. You'd have to do a bit of work to pull it out though.

http://kohanaframework.org/

It will allow you to do error logging to a file and email a recipient. It also enables you to redirect to your friendly error page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文