asjustas/guzzle-logger-middleware

Guzzle middleware to log requests and responses

1.0.3 2020-10-29 05:52 UTC

This package is auto-updated.

Last update: 2024-10-29 04:53:36 UTC


README

Latest Stable Version Latest Unstable Version

Guzzle middleware to log requests and responses.

Installation

Install via composer:

composer require asjustas/guzzle-logger-middleware

Usage

<?php

use GuzzleHttp\HandlerStack;
use AJ\Guzzle\Middleware\Logger;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Client;

$stack = HandlerStack::create();
$stack
    ->push(
        new Logger(
            function (RequestInterface $request, ?ResponseInterface $response, array $context) {
                print_r(
                    [
                        'uri' => (string)$request->getUri(),
                        'req' => (string)$request->getBody(),
                        'res' => $response ? (string)$response->getBody() : '',
                        'context' => $context,
                    ]
                );
            }
        )
    );

$client = new Client([
    'handler' => $stack,
]);

$client
    ->request(
        'POST',
        'http://example.com/404',
        [
            'body' => 'Hello World',
            'logger' => [
                'enabled' => true,
                'context' => [
                    'something' => 'important',
                ],
            ],
        ]
    );

Based upon