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

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

frnde/guzzle\_logger
====================

A Guzzle middleware to log request and responses automatically

v3.0.2(1y ago)12.8k↓44.4%[1 PRs](https://github.com/frnde/guzzle-log-middleware/pulls)MITPHPPHP ^8.0

Since Feb 6Pushed 1y ago1 watchersCompare

[ Source](https://github.com/frnde/guzzle-log-middleware)[ Packagist](https://packagist.org/packages/frnde/guzzle_logger)[ Docs](https://github.com/frnde/guzzle-log-middleware)[ RSS](/packages/frnde-guzzle-logger/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (9)Versions (6)Used By (0)

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

[](#guzzle-log-middleware)

This is a fork of the repository  by George Mponos.

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 7 of Guzzle.

Install
-------

[](#install)

Via Composer

```
$ composer require frnde/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:

```
