the-toster / http-message-logger
Log PSR MessageInterface instances to LoggerInterface
Installs: 1 854
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7.4|^8.0
- ext-json: *
- psr/http-message: ^1.0
- psr/log: *
Requires (Dev)
- guzzlehttp/psr7: ^1.7
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.3
This package is auto-updated.
Last update: 2024-10-10 15:09:07 UTC
README
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
🙂