如果你想尝试表达我建议使用骨架安装程序。它为您提供了安装内容的选项。其中一个选项是 whoops 错误处理程序,它提供了许多有关异常的详细信息。
官方文档在这里有很多信息:https ://docs.zendframework.com/zend-expressive/
安装程序文档:https ://docs.zendframework.com/zend-expressive/getting-started/skeleton/
更新:添加ErrorHandler记录器示例
作为 ErrorHandler 的基础,您可以使用Zend\Stratigility\Middleware\ErrorHandler
. 您可以将侦听器附加到该 ErrorHandler 并将其用于日志记录。或者,您可以复制该类并根据需要对其进行修改。
下一步是为它创建一个 ErrorHandlerFactory:
<?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;
}
}
之后,您需要注册 ErrorHandler。为此,您可以将其添加到该部分config/autoload/middleware-pipeline.global.php
并特别添加到该middleware => always
部分中。这样它就会一直运行。如果您首先注册它,它将在其他任何东西之前运行。
<?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,
],
/* ... */
],
];