PHPackages                             softonic/guzzle\_logger - 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. softonic/guzzle\_logger

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

softonic/guzzle\_logger
=======================

A Guzzle middleware to log request and responses automatically

v2.2.1(2y ago)03.5k↓82.2%MITPHPPHP ^7.2 || ^8.0

Since Apr 5Pushed 2y agoCompare

[ Source](https://github.com/softonic/guzzle-log-middleware)[ Packagist](https://packagist.org/packages/softonic/guzzle_logger)[ Docs](https://github.com/softonic/guzzle-log-middleware)[ RSS](/packages/softonic-guzzle-logger/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (1)Dependencies (9)Versions (15)Used By (0)

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

[](#guzzle-log-middleware)

[![codecov](https://camo.githubusercontent.com/88b095dec39a68258469beb06a19d66b9138b81e64e4d79af0fbc578960c18ce/68747470733a2f2f636f6465636f762e696f2f67682f736f66746f6e69632f67757a7a6c652d6c6f672d6d6964646c65776172652f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/softonic/guzzle-log-middleware)[![Total Downloads](https://camo.githubusercontent.com/54b27805cf828d173cb7c9dd4722bdd453a3c56dd1a0386659e6f91b874a3751/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736f66746f6e69632f67757a7a6c655f6c6f676765722e737667)](https://packagist.org/packages/softonic/guzzle_logger)[![Build Status](https://camo.githubusercontent.com/2ea9b6aaed33a0808eb2392c3a838b1bab009a6e455f865c70104eacb9f21e26/68747470733a2f2f7472617669732d63692e6f72672f736f66746f6e69632f67757a7a6c652d6c6f672d6d6964646c65776172652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/softonic/guzzle-log-middleware)[![MIT licensed](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](https://github.com/softonic/monolog-slack/blob/master/LICENSE.md)

This is a middleware for [guzzle](https://github.com/guzzle/guzzle) that will help you automatically log every request and response using a PSR-3 logger.

The middleware is functional with version 6 of Guzzle.

Install
-------

[](#install)

Via Composer

```
$ composer require softonic/guzzle_logger
```

Usage
-----

[](#usage)

### Simple usage

[](#simple-usage)

```
use GuzzleLogMiddleware\LogMiddleware;
use GuzzleHttp\HandlerStack;

$logger = new Logger();  //A new PSR-3 Logger like Monolog
$stack = HandlerStack::create(); // will create a stack stack with middlewares of guzzle already pushed inside of it.
$stack->push(new LogMiddleware($logger));
$client = new GuzzleHttp\Client([
    'handler' => $stack,
]);
```

From now on each request and response you execute using `$client` object will be logged. By default the middleware logs every activity with level `DEBUG`.

### Advanced initialization

[](#advanced-initialization)

The signature of the `LogMiddleware` class is the following:

```
\GuzzleLogMiddleware\LogMiddleware(
    \Psr\Log\LoggerInterface $logger,
    \GuzzleLogMiddleware\Handler\HandlerInterface $handler = null,
    bool $onFailureOnly = false,
    bool $logStatistics = false
);
```

- **logger** - The PSR-3 logger to use for logging.
- **handler** - A HandlerInterface class that will be responsible for logging your request/response. Check Handlers sections.
- **onFailureOnly** - By default the middleware is set to log every request and response. If you wish to log the HTTP messages only when guzzle returns a rejection set this as true or when an exception occurred. Guzzle returns a rejection when [http\_errors](http://docs.guzzlephp.org/en/stable/request-options.html#http-errors) option is set to true.
- **logStatistics** - If you set this option as true then the middleware will also log statistics about the HTTP transaction.

### Handlers

[](#handlers)

In order to make the middleware more flexible we allow the developer to initialize it with a handler. A handler is the class that will be responsible for logging the HTTP message and it must implement a `HandlerInterface`.

As an example let's say that we create the following handler:

```
