Zend\Stratigility\Middleware\ErrorHandler::attachListener PHP Method

attachListener() public method

Each listener receives the following three arguments: - Throwable|Exception $error - ServerRequestInterface $request - ResponseInterface $response These instances are all immutable, and the return values of listeners are ignored; use listeners for reporting purposes only.
public attachListener ( callable $listener )
$listener callable
    public function attachListener(callable $listener)
    {
        if (in_array($listener, $this->listeners, true)) {
            return;
        }
        $this->listeners[] = $listener;
    }

Usage Example

 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;
 }