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

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

covergenius/guzzle\_logger
==========================

A Guzzle middleware to log request and responses automatically

v2.3.0(1y ago)378.2k↓34.5%1MITPHPPHP ^7.2 || ^8.0CI failing

Since Apr 5Pushed 1y ago1 watchersCompare

[ Source](https://github.com/CoverGenius/guzzle-log-middleware)[ Packagist](https://packagist.org/packages/covergenius/guzzle_logger)[ Docs](https://github.com/covergenius/guzzle-log-middleware)[ GitHub Sponsors](https://github.com/gmponos)[ RSS](/packages/covergenius-guzzle-logger/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (10)Versions (17)Used By (0)

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

[](#guzzle-log-middleware)

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 covergenius/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:

```
