Sorry if this is a too simple thing to ask for here. but i am stuck here and flooded with all the information available , where I am only trying to write a basic error logging mechanism that logs all my PHP errors and exception to a log file as of now i am using Zend server and Z-ray is enabled which is the only way for displaying the errors on the Z-ray bar.
Also throw me some light on Errorlistener and Delegators . Can it be used to log error, tried to implement it but no luck.
LoggingErrorListener.php
namespace App\ErrorListner;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Throwable;
class LoggingErrorListener
{
private const LOG_STRING = '{status} [{method}] {uri}: {error}';
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function __invoke(
Throwable $error,
ServerRequestInterface $request,
ResponseInterface $response
) {
$this->logger->error(sprintf(
self::LOG_FORMAT,
$response->getStatusCode(),
$request->getMethod(),
(string) $request->getUri(),
$error->getMessage()
));
}
}
LoggingErrorListenerelegator.php
namespace App\ErrorListner;
use Laminas\Stratigility\Middleware\ErrorHandler;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
class LoggingErrorListenerDelegator
{
public function __invoke(ContainerInterface $container,string $serviceName,callable $callback): ErrorHandler
{
$errorHandler = $callback();
$errorHandler->attachListener(
new LoggingErrorListener($container->get(LoggerInterface::class))
);
return $errorHandler;
}
}
ConfigProvider.php
i have added this
'delegators' => [
ErrorHandler::class => [
ErrorListner\LoggingErrorListenerDelegator::class,
],
],
And how to test if the delegator is working. because right now i think it is not getting detected.