- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 register_shutdown_function 和 set_error_handler 函数记录错误。我正在使用以下类文件。但它不起作用。
<?php
//Logs.php
class MyLogs
{
public function __construct()
{
register_shutdown_function(array($this, 'shutdownHandler'));
set_error_handler(array($this, 'errorHandler'));
}
private function errorHandler($error_level, $error_message, $error_file, $error_line, $error_context)
{
$error = "lvl: " . $error_level . " | msg:" . $error_message . " | file:" . $error_file . " | ln:" . $error_line;
switch ($error_level) {
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_PARSE:
case E_USER_ERROR:
$this->logMe($error, "fatal");
break;
case E_USER_ERROR:
case E_RECOVERABLE_ERROR:
$this->logMe($error, "error");
break;
case E_WARNING:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_USER_WARNING:
$this->logMe($error, "warn");
break;
case E_NOTICE:
case E_USER_NOTICE:
$this->logMe($error, "info");
break;
case E_STRICT:
$this->logMe($error, "debug");
break;
default:
$this->logMe($error, "warn");
}
}
private function shutdownHandler() //will be called when php script ends.
{
$lasterror = error_get_last();
echo "Level: " . $lasterror;
switch ($lasterror['type'])
{
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
case E_RECOVERABLE_ERROR:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_PARSE:
$error = "[SHUTDOWN] lvl:" . $lasterror['type'] . " | msg:" . $lasterror['message'] . " | file:" . $lasterror['file'] . " | ln:" . $lasterror['line'];
$this->logMe($error, "fatal");
}
}
private function logMe($error, $errlvl)
{
echo 'Error No: ' . $error . ' <BR> Error Level: ' .$errlvl;
}
}
..
<?php
// Index.php
include "Logs.php"
new MyLogs();
echo $b; //this should generate a notice error since $b does not exits...
但是,如果我不使用类而只使用函数,它们就可以正常工作......有人可以告诉我出了什么问题吗?
谢谢
最佳答案
您应该正确设置错误设置。例如
ini_set('error_reporting', -1);
ini_set('display_errors', 1);
在此之后,您会发现错误:您的 shutdownHandler
和 errorHandler
必须是公开的。
关于php - 在类中调用register_shutdown_function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23590875/
这是我正在测试以处理错误的原始代码的片段。 第一种方法是拨打 fatalErrorHandler() ,但第二种方法不是调用 fatalErrorHandler() .可能是什么原因? 第一 out
当脚本出现 fatal error 时,我做了一个自定义重定向功能: 这在所有页面之前包含的前置脚本上调用: register_shutdown_function("shutdownHandler")
我有一个 PHP 类,可以动态创建 PNG 图像并将其发送到浏览器。 PHP 手册说我需要确保最后调用 imagedestroy 函数来释放内存。现在,如果我不使用类,我会有这样的代码: functi
我正在尝试用自定义函数替换内置的 php shutdown_function。 它工作得很好,但是,它仍然在我的新错误消息上方输出原始错误(内置错误)。 有什么想法吗? 最佳答案 正如评论中已经
是否可以覆盖已经设置的 register_shutdown_function 堆栈?像这样的东西: function f1(){ echo "f1"; } function f2(){
我目前出于多种目的使用 register_shutdown_function()。一种用途是处理 fatal error ,另一种用途是记录执行期间使用的资源,如时间、内存使用情况等。 目前我注册了两
我正在尝试使用一个关闭函数,该函数使用注册后动态更改的值。我的理解是,通过引用传递变量将允许代码后面部分的更改影响关闭调用。下面的代码证明情况并非如此: 我是不是误解了应该怎么做?将引用传递转换为值
我是 php 新手。我开始了解 php 中的 register_shutdown_function()。我在 php.net 手册中读到了这个函数,当脚本完成执行时这个函数调用。但是我有一个问题 如果
当从命令行(通过 Ctrl+c)或使用 kill 命令终止 cron 进程时,是否可以调用函数? 我已经尝试过register_shutdown_function(),但它似乎并没有在脚本被杀死时被调
通过 register_shutdown_function 方法,可以让我们设置一个当执行关闭时可以被调用的另一个函数。 也就是说,当我们的脚本执行完成或者意外死掉导致 php 执行即将关闭时,我
我试图捕捉 Zend Framework 应用程序中发生的 fatal error 。我试图从 Bootstrap 中调用 register_shutdown_function: protected
根据手册http://php.net/manual/en/function.register-shutdown-function.php ,register_shutdown_function() 实
根据手册http://php.net/manual/en/function.register-shutdown-function.php ,register_shutdown_function() 实
我正在尝试找到一种干净的方法来处理致命和严重错误(即没有输出缓冲),并且我读到在 register_shutdown_function 中使用 HTTP header 是不可能的。 问题是,当发生严重
我想弄清楚为什么php需要1整秒钟来执行register_shutdown_function。如果有办法克服的话。 "; } register_shutdown_function('shutdown'
我有方法,在这个方法中可能会发生 fatal error ,为了捕获这个错误我做了这个 class a { function shutDownFunction() { $err
如果我的脚本在 register_shutdown_function() 中被 die() 取消了,我能以某种方式检查吗? ? 像这样: register_shutdown_function('shu
我正在尝试使用 register_shutdown_function 来“捕获”PHP fatal error ,以便我可以返回一个干净的错误消息。 在一个简单的 PHP 文件中,以下两个测试用例都得
如果我在同一个脚本中定义了以下内容: register_shutdown_function('handlerOne'); set_error_handler('handlerTwo'); 是否有任何错
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我是一名优秀的程序员,十分优秀!