Zend expressive - php error reporting

徘徊边缘 提交于 2019-12-06 06:20:21

If you want to try expressive I suggest to use the skeleton installer. It gives you options on what to install. One of the options is the whoops error handler which gives a lot of detailed info about exceptions.

The official docs are here with a lot of info: https://docs.zendframework.com/zend-expressive/

The installer docs: https://docs.zendframework.com/zend-expressive/getting-started/skeleton/

Update: Add ErrorHandler logger example

As a base for your ErrorHandler you can use Zend\Stratigility\Middleware\ErrorHandler. You can attach a listener to that ErrorHandler and use it for logging. Alternatively you can copy that class and modify it to your needs.

Next step is creating a ErrorHandlerFactory for it:

<?php 
// src/Factory/ErrorHandler/ErrorHandlerFactory.php

namespace App\Factory\ErrorHandler;

use Interop\Container\ContainerInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Throwable;
use Zend\Diactoros\Response;
use Zend\Expressive\Middleware\ErrorResponseGenerator;
use Zend\Stratigility\Middleware\ErrorHandler;

class ErrorHandlerFactory
{
    public function __invoke(ContainerInterface $container)
    {
        $generator = $container->has(ErrorResponseGenerator::class)
            ? $container->get(ErrorResponseGenerator::class)
            : null;

        $errorHandler = new ErrorHandler(new Response(), $generator);

        if ($container->has(LoggerInterface::class)) {
            $logger = $container->get(LoggerInterface::class);
            $errorHandler->attachListener(function (
                Throwable $throwable,
                RequestInterface $request,
                ResponseInterface $response
            ) use ($logger) {
                $logger->error('"{method} {uri}": {message} in {file}:{line}', [
                    'date'    => date('Y-m-d H:i:s'),
                    'method'  => $request->getMethod(),
                    'uri'     => (string) $request->getUri(),
                    'message' => $throwable->getMessage(),
                    'file'    => $throwable->getFile(),
                    'line'    => $throwable->getLine(),
                ]);
            });
        }

        return $errorHandler;
    }
}

After that you need to register the ErrorHandler. You do this by adding it to config/autoload/middleware-pipeline.global.php and specifically in the middleware => always section. This way it will always run. If you register it as first, it will run before anything else.

<?php 
// in config/autoload/middleware-pipeline.global.php

use Acme\Container\ErrorHandlerFactory;
use Zend\Stratigility\Middleware\ErrorHandler;

return [
    'dependencies' => [
        /* ... */
        'factories' => [
            ErrorHandler::class => ErrorHandlerFactory::class,
            /* ... */
        ],
        /* ... */
    ],
    'middleware_pipeline' => [
        'always' => [
            'middleware' => [
                ErrorHandler::class,
                /* ... */
            ],
            'priority' => 10000,
        ],
        /* ... */
    ],
];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!