the-toster/http-message-logger

Log PSR MessageInterface instances to LoggerInterface

0.0.4 2021-12-10 08:54 UTC

This package is auto-updated.

Last update: 2024-12-10 15:28:57 UTC


README

Build Status Psalm coverage

Logger for HTTP messages

composer require the-toster/http-message-logger 

Small logger to debug your API's.
You can use it like this:

use HttpMessageLogger\LoggerFactory;
/** @var \Psr\Log\LoggerInterface $psrLogger */
$logger = LoggerFactory::default($psrLogger, 'Prefix for log message');

/** @var \Psr\Http\Message\RequestInterface $request */
$logger->logRequest($request); 

// ... doing stuff, calculating response 

/** @var \Psr\Http\Message\ResponseInterface $response */
$logger->logResponse($response, $request); 

It will dump full $request & $response to your $psrLogger, tagged with corresponding request hash.

Also, you can instantiate it with your own formatter and, optionally, default log level:

use HttpMessageLogger\Logger;
use HttpMessageLogger\Formatter\DefaultFormatter;
// ...

/** @var \Psr\Log\LoggerInterface $psrLogger */
$logger = new Logger($psrLogger, new DefaultFormatter('Prefix for log message'), $logLevel);

You can also omit request when logging response, and you can pass log level:

$logger->logResponse($response);
$logger->logResponse($response, $request, 'info');

To customize logging format, just implements your own HttpMessageLogger\Formatter\FormatterInterface 🙂