PHPackages                             malvik-lab/guzzle-log-middleware - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. malvik-lab/guzzle-log-middleware

ActiveLibrary[HTTP &amp; Networking](/categories/http)

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(3y ago)036MITPHPPHP ^8.0.2

Since Dec 23Pushed 3y ago1 watchersCompare

[ Source](https://github.com/malvik-lab/guzzle-log-middleware)[ Packagist](https://packagist.org/packages/malvik-lab/guzzle-log-middleware)[ RSS](/packages/malvik-lab-guzzle-log-middleware/feed)WikiDiscussions main Synced 2d ago

READMEChangelogDependencies (8)Versions (9)Used By (0)

Guzzle Log Middleware
=====================

[](#guzzle-log-middleware)

Log every request and response of your [Guzzle client](https://github.com/guzzle/guzzle). You can set different adapters according to your needs:

- [PSR-3 Logger Interface](https://www.php-fig.org/psr/psr-3)
- [PSR-6 Caching Interface](https://www.php-fig.org/psr/psr-6)
- Filesystem
- Custom adapter

Installation
------------

[](#installation)

```
$ composer require malvik-lab/guzzle-log-middleware

```

Prepare
-------

[](#prepare)

#### With PSR-3 Logger Interface

[](#with-psr-3-logger-interface)

###### In this example I have chosen [Laminas Log](https://github.com/laminas/laminas-log)

[](#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
-------

[](#prepare-1)

#### With PSR-6 Caching Interface

[](#with-psr-6-caching-interface)

###### In this example I have chosen [Laminas Cache](https://github.com/laminas/laminas-cache) and Redis

[](#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
-------

[](#prepare-2)

#### With Filesystem

[](#with-filesystem)

###### In this example I have chosen to save each request and response in a single separate file

[](#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
-------

[](#prepare-3)

#### With Filesystem

[](#with-filesystem-1)

###### In this example I have chosen to save all requests and responses in one file

[](#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
-----

[](#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
--------------

[](#output-example)

```
>>>>>>>>
POST /app/login HTTP/1.1
Content-Length: 74
User-Agent: GuzzleHttp/7
Host: localhost

{username":"me","password":"my-password"}
