PHPackages                             chaodada/guzzle\_log - 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. chaodada/guzzle\_log

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

chaodada/guzzle\_log
====================

一个Guzzle中间件，用于自动记录请求和响应

3.4.0(2y ago)03.1k↑159.8%MITPHPPHP ^7.2 || ^8.0

Since Apr 5Pushed 2y ago1 watchersCompare

[ Source](https://github.com/chaodada/guzzle_log)[ Packagist](https://packagist.org/packages/chaodada/guzzle_log)[ Docs](https://github.com/gmponos/guzzle-log-middleware)[ GitHub Sponsors](https://github.com/gmponos)[ RSS](/packages/chaodada-guzzle-log/feed)WikiDiscussions main Synced 1mo ago

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

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

[](#guzzle-log-middleware)

[![codecov](https://camo.githubusercontent.com/fd47554fa1cb9b3d0b4443a4383e40a578cfeeb37d4074a12eb1e8bf174cb24a/68747470733a2f2f636f6465636f762e696f2f67682f676d706f6e6f732f67757a7a6c652d6c6f672d6d6964646c65776172652f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/gmponos/guzzle-log-middleware)[![Total Downloads](https://camo.githubusercontent.com/173caa424c6734591f412659ec1d33e7cf5cdc3b32cf8b3defeb95276737f130/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f676d706f6e6f732f67757a7a6c655f6c6f676765722e737667)](https://packagist.org/packages/gmponos/guzzle_logger)[![Build Status](https://camo.githubusercontent.com/a74063f9a25dc56243c4961b9f31e9aa920a71744249b03f66f66f7198fdf25e/68747470733a2f2f7472617669732d63692e6f72672f676d706f6e6f732f67757a7a6c652d6c6f672d6d6964646c65776172652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/gmponos/guzzle-log-middleware)[![MIT licensed](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](https://github.com/gmponos/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 gmponos/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:

```
