0

使用 Zend Expressive 2.0.5,我想将 PHP 错误记录到 PHP 的错误日志文件中,我浏览了https://docs.zendframework.com/zend-expressive/features/error-handling/#handling-exceptions-and -错误

我还看到了这篇文章:Zend expressive - php error reporting。这为我清除了很多东西,但仍然没有解决所提出的问题。

我所做的事情:定义了我自己的ErrorHandlerFactory,我将两个听众附加到Zend-stratigility's ErrorHandler

  1. First Listener 使用 Zend Log 登录到我的应用程序的日志文件。(只是认为在我的 application.log 中也有错误会很好。)

  2. 在第二个监听器中,我想登录到 PHP 的错误日志文件,所以我使用了 php.ini 的error_log()方法。

问题:

  1. error_log() 不会以 php 的错误处理程序打印日志的方式打印日志。我的意思是说:

    当 php 的错误处理程序打印错误时,它看起来像这样:

    [08-Feb-2018 08:22:51 US/Central] PHP 警告:array_push() 需要至少 2 个参数,1 个在 C:\webserver\webroot\myapi\src\App\src\Action\PageAction.php 中给出在第 38 行

    当我使用它打印日志时,error_log()它看起来像这样:

    [08-Feb-2018 09:03:49 US/Central] array_push() 至少需要 2 个参数,1 个在 C:\webserver\webroot\myapi\src\App\src\Action\PageAction.php 第 38 行给出

    我在这里缺少的是 PHP 的错误类型:PHP 警告,这是错误代码吗?我得到的错误代码是一个整数,我该如何解析该代码?我是否应该将错误代码与出现在日志中的 PHP 错误常量进行映射,例如:WARNING、NOTICE 等,我什至可以这样做,但问题是:当 php 错误时,我得到相同的错误代码0处理程序打印了一个警告和一个致命错误日志。

  2. 像这样在 PHP 的错误日志文件中记录错误是否正确?我应该做 PHP 错误处理程序的工作吗?错误处理程序可能会做很多事情,例如:记录少数错误的错误消息,但另一个也记录堆栈跟踪。如果这不正确,那么我还能如何将错误发送到 PHP 的 error_handler?

    据我了解:

    我自己的错误处理程序阻止用户查找异常和堆栈跟踪,而是返回一般消息。这也意味着错误处理程序会处理错误并且不会将其抛到更远的地方,即不会将其抛给 PHP 的错误处理程序。

4

1 回答 1

1

回答问题1:

我几乎可以模拟 PHP 错误处理程序记录 PHP 错误的方式。我做的事情:

  1. 浏览了文档这个SO question。使用这些我能够将侦听器附加到 Zend-stratigility 的 ErrorHandler
  2. 浏览了 PHP 的错误常量set_error_handler(),这给了我一些关于如何找出发生了哪种类型的错误或异常的想法。

下面是我附加侦听器的 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(listener) 可以复制消息并使用 记录到 PHP 的错误日志中error_log(),那就没问题了。

于 2018-02-09T13:15:48.170 回答