2017-05-23 12 views
1

Zend Expressive 2すべてのリクエストと応答をログに記録する方法

すべての入力とすべての出力が必要です。 私、pipeline.php

$app->pipe(\Zend\Stratigility\Middleware\ErrorHandler::class); 
$app->pipe(\Zend\Expressive\Helper\ServerUrlMiddleware::class); 
$app->pipe(\App\Middleware\LogRequestsMiddleware::class); 
$app->pipeRoutingMiddleware(); 
$app->pipe(\Zend\Expressive\Middleware\ImplicitHeadMiddleware::class); 
$app->pipe(\Zend\Expressive\Middleware\ImplicitOptionsMiddleware::class); 
$app->pipe(\Zend\Expressive\Helper\UrlHelperMiddleware::class); 
$app->pipeDispatchMiddleware(); 
$app->pipe(\App\Middleware\LogResponseMiddleware::class); 
$app->pipe(\Zend\Expressive\Middleware\NotFoundHandler::class); 

LogResponseMiddlewareが実行いけません。エラーの場合はそれも必要です。

答えて

0

正しい順序:ここ

$app->pipe(\App\Middleware\LogResponseMiddleware::class); 
$app->pipe(\Zend\Stratigility\Middleware\ErrorHandler::class); 
$app->pipe(\Zend\Expressive\Helper\ServerUrlMiddleware::class); 
$app->pipeRoutingMiddleware(); 
$app->pipe(\Zend\Expressive\Middleware\ImplicitHeadMiddleware::class); 
$app->pipe(\Zend\Expressive\Middleware\ImplicitOptionsMiddleware::class); 
$app->pipe(\Zend\Expressive\Helper\UrlHelperMiddleware::class); 
$app->pipeDispatchMiddleware(); 
$app->pipe(\App\Middleware\LogRequestsMiddleware::class); 
$app->pipe(\Zend\Expressive\Middleware\NotFoundHandler::class); 

詳細:あなたが使用することができ、あなたの工場で

<?php 
namespace Acme\Middleware; 

use Psr\Log\LoggerInterface; 
use Interop\Http\ServerMiddleware\DelegateInterface; 
use Interop\Http\ServerMiddleware\MiddlewareInterface; 
use Psr\Http\Message\ResponseInterface; 
use Psr\Http\Message\ServerRequestInterface; 

class LoggingMiddleware implements MiddlewareInterface 
{ 
    /** 
    * @var Psr\Log\LoggerInterface $logger 
    */ 
    private $logger; 

    public function __construct(LoggerInterface $logger) 
    { 
     $this->logger = $logger; 
    } 

    public function process(ServerRequestInterface $request, DelegateInterface $delegate) { 
     // log the request 
     $this->logger->log(Logger::INFO, 'Request', []); 

     $response = $delegate->process($request); 

     // log the response 
     $this->logger->log(Logger::INFO, 'Response', []); 

     return $response; 
    } 
} 

https://framework.zend.com/blog/2017-03-15-nested-middleware-in-expressive.html

0

あなただけのミドルウェアを作成し、リクエストとレスポンスが非常に簡単にログインすることができますモノログのようなPSRロガー:

<?php 
namespace Acme\Middleware; 

use Interop\Container\ContainerInterface; 
use Psr\Log\LoggerInterface; 

class LoggingMiddlewareFactory 
{ 
    public function __invoke(ContainerInterface $container) 
    { 
     $logger = $container->get(LoggerInterface::class); 

     return new LoggingMiddleware($logger); 
    } 
} 

最後に、ログミドルウェアはまさにServerUrlMiddleware後にパイプに追加する必要があります。それだけです

<?php 
// config/pipeline.php 
$app->pipe(ErrorHandler::class); 
$app->pipe(ServerUrlMiddleware::class); 

$app->pipe(LoggingMiddleware::class); 

を。

関連する問題