使用ZendExpression2.0.5,我想将PHP错误记录到PHP的错误日志文件中,我浏览了https://docs.zendframework.com/zend-expressive/features/error-handling/#handling-exceptions-and-errors
我还看到了这个帖子:Zend expressive错误报告。这为我澄清了很多问题,但仍然没有解决问题。
我所做的事情:定义了我自己的ErrorHandlerFactory,其中我将两个侦听器附加到Zend-stratigility's ErrorHandler
error_log()方法。问题:
当我使用error_log()打印日志时,它看起来如下所示:
08-2018年09-2018年09:03:49 US/Central array_push()要求至少有2个参数,第38行的C:\webserver\webroot\myapi\src\App\src\Action\PageAction.php中有1个参数
我在这里缺少的是PHP的错误类型:PHP警告,这是错误代码吗?我得到的错误代码是一个整数,我如何解析该代码?如果我用出现在日志中的PHP常量映射错误代码,例如:警告、注意事项等,我甚至可以这样做,但问题是:在php的错误处理程序打印警告和致命错误日志时,我得到了的相同错误代码。
发布于 2018-02-09 13:15:48
回答问题1:
我几乎能够模拟PHP错误处理程序记录PHP错误的方式。我做过的事:
下面是我的ErrorHandlerFactory的代码,我在这里附加了侦听器。
<?php
// TODO: PHP 7.0.8 is giving strict erros eben if this directive is not enabled. And that too, it should be enabled per file from my understanding.
//declare(strict_types = 1);
namespace App\Factories;
use Interop\Container\ContainerInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\Log\Logger as ZendLogger;
use Throwable;
use Zend\Diactoros\Response;
use Zend\Expressive\Middleware\ErrorResponseGenerator;
use Zend\Stratigility\Middleware\ErrorHandler;
class ErrorHandlerFactory
{
/**
* @param ContainerInterface $container
* @return ErrorHandler
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
public function __invoke(ContainerInterface $container)
{
$generator = $container->has(ErrorResponseGenerator::class)
? $container->get(ErrorResponseGenerator::class)
: null;
$errorHandler = new ErrorHandler(new Response(), $generator);
// attaching a listener for logging into application's log file.
if ($container->has(ZendLogger::class)) {
/** @var ZendLogger $logger */
$logger = $container->get(ZendLogger::class);
$errorHandler->attachListener(function (
Throwable $throwable,
RequestInterface $request,
ResponseInterface $response
) use ($logger) {
$logger->err(NULL, [
'method' => $request->getMethod(),
'uri' => (string) $request->getUri(),
'message' => $throwable->getMessage(),
'file' => $throwable->getFile(),
'line' => $throwable->getLine(),
]);
});
}
// Attaching second listener for logging the errors into the PHP's error log
$errorHandler->attachListener(function (
Throwable $throwable,
RequestInterface $request,
ResponseInterface $response
) {
// Default Error type, when PHP Error occurs.
$errorType = sprintf("Fatal error: Uncaught %s", get_class($throwable));
if (get_class($throwable) === "ErrorException") {
// this is an Exception
/** @noinspection PhpUndefinedMethodInspection */
$severity = $throwable->getSeverity();
switch($severity) {
case E_ERROR:
case E_USER_ERROR:
$errorType = 'Fatal error';
break;
case E_USER_WARNING:
case E_WARNING:
$errorType = 'Warning';
break;
case E_USER_NOTICE:
case E_NOTICE:
case E_STRICT:
$errorType = 'Notice';
break;
case E_RECOVERABLE_ERROR:
$errorType = 'Catchable fatal error';
break;
case E_USER_DEPRECATED:
case E_DEPRECATED:
$errorType = "Deprecated";
break;
default:
$errorType = 'Unknown error';
}
error_log(sprintf("PHP %s: %s in %s on line %d", $errorType, $throwable->getMessage(), $throwable->getFile(), $throwable->getLine()), 0);
}
else {
// this is an Error.
error_log(sprintf("PHP %s: %s in %s on line %d \nStack trace:\n%s", $errorType, $throwable->getMessage(), $throwable->getFile(), $throwable->getLine(), $throwable->getTraceAsString()), 0);
}
});
return $errorHandler;
}
}除此之外,这个工厂还需要添加到依赖项中。在文件:dependencies.global.php中,在factories数组中:
替换
Zend\Stratigility\Middleware\ErrorHandler::class => Container\ErrorHandlerFactory::class,
使用
Zend\Stratigility\Middleware\ErrorHandler::class => \App\Factories\ErrorHandlerFactory::class这几乎可以模拟php错误处理程序的日志记录行为。
回答问题2:
我认为这样做很好,因为PHP本身提供了set_error_handler(),而且无论如何,我们必须自己处理错误,而不是将它传递给PHP的错误处理程序。如果我们的ErrorHandler(侦听器)能够复制消息并使用error_log()登录到PHP的错误日志中,那么就可以了。
https://stackoverflow.com/questions/48689174
复制相似问题