asjustas / guzzle-logger-middleware
Guzzle middleware to log requests and responses
Installs: 10 062
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.1
- guzzlehttp/guzzle: ^6.0 || ^7.0
README
Guzzle middleware to log requests and responses.
Installation
Install via composer:
composer require asjustas/guzzle-logger-middleware
Usage
<?php use GuzzleHttp\HandlerStack; use AJ\Guzzle\Middleware\Logger; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use GuzzleHttp\Client; $stack = HandlerStack::create(); $stack ->push( new Logger( function (RequestInterface $request, ?ResponseInterface $response, array $context) { print_r( [ 'uri' => (string)$request->getUri(), 'req' => (string)$request->getBody(), 'res' => $response ? (string)$response->getBody() : '', 'context' => $context, ] ); } ) ); $client = new Client([ 'handler' => $stack, ]); $client ->request( 'POST', 'http://example.com/404', [ 'body' => 'Hello World', 'logger' => [ 'enabled' => true, 'context' => [ 'something' => 'important', ], ], ] );