malvik-lab / guzzle-log-middleware
Log every request and response of your Guzzle client. You can set different adapters according to your needs.
1.0.4
2023-02-24 10:07 UTC
Requires
- php: ^8.0.2
- guzzlehttp/guzzle: ^7.0
- psr/cache: ^3.0
- psr/log: ^3.0
Requires (Dev)
- laminas/laminas-cache: 3.0.x-dev
- laminas/laminas-log: 2.14.x-dev
- laminas/laminas-serializer: 2.11.x-dev
- phpunit/phpunit: ^9.5
- symfony/filesystem: 5.x-dev
README
Log every request and response of your Guzzle client. You can set different adapters according to your needs:
- PSR-3 Logger Interface
- PSR-6 Caching Interface
- Filesystem
- Custom adapter
Installation
$ composer require malvik-lab/guzzle-log-middleware
Prepare
With PSR-3 Logger Interface
In this example I have chosen Laminas Log
// composer require laminas/laminas-log laminas/laminas-serializer $writer = new \Laminas\Log\Writer\Stream('/path/to/log/file.log'); $laminasLogLogger = new \Laminas\Log\Logger(); $laminasLogLogger->addWriter($writer); $log = new \Laminas\Log\PsrLoggerAdapter($laminasLogLogger); $adapter = [ 'adapter' => $log, 'options' => [ 'template' => \GuzzleHttp\MessageFormatter::CLF, // Not mandatory. For more information: https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php ] ];
Prepare
With PSR-6 Caching Interface
In this example I have chosen Laminas Cache and Redis
// composer require laminas/laminas-cache laminas/laminas-serializer $storage = \Laminas\Cache\StorageFactory::factory([ 'adapter' => [ 'name' => 'redis', 'options' => [ 'namespace' => '', 'namespace_separator' => '_', 'ttl' => 3600, 'password' => 'my-redis-password', 'server' => [ 'host' => 'my-redis-host', 'port' => 6379, ], ], ], 'plugins' => [ 'serializer', ], ]); $cache = new \Laminas\Cache\Psr\CacheItemPool\CacheItemPoolDecorator($storage); $adapter = [ 'adapter' => $cache, 'options' => [ 'keyPrefix' => 'my-redis-key-prefix', // Not mandatory 'template' => \GuzzleHttp\MessageFormatter::CLF, // Not mandatory. For more information: https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php ] ];
Prepare
With Filesystem
In this example I have chosen to save each request and response in a single separate file
$adapter = [ 'adapter' => 'filesystem', 'options' => [ 'dirPath' => '/path/to/log/folder', 'template' => \GuzzleHttp\MessageFormatter::CLF, // Not mandatory. For more information: https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php ] ];
Prepare
With Filesystem
In this example I have chosen to save all requests and responses in one file
$adapter = [ 'adapter' => 'filesystem', 'options' => [ 'filePath' => '/path/to/log/file.log', 'template' => \GuzzleHttp\MessageFormatter::CLF, // Not mandatory. For more information: https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php ] ];
Usage
// In the constructor you can inject one or more adapters $guzzleLogMiddleware = new \MalvikLab\GuzzleLogMiddleware\GuzzleLogMiddleware([ $adapter ]); $stack = \GuzzleHttp\HandlerStack::create(); $stack->push($guzzleLogMiddleware); // your client $client = new \GuzzleHttp\Client([ 'handler' => $stack ]);
Output Example
>>>>>>>>
POST /app/login HTTP/1.1
Content-Length: 74
User-Agent: GuzzleHttp/7
Host: localhost
{username":"me","password":"my-password"}
<<<<<<<<
HTTP/1.1 200 OK
Date: Mon, 21 Dec 2020 13:58:26 GMT
Server: Apache/2.4.46 (Ubuntu)
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Access-Control-Allow-Origin: *
Content-Length: 284
Content-Type: application/json
{"token": "random-token","expireDatetime": "2020-12-21T15:28:26+01:00"}
--------
NULL