PHP code example of malvik-lab / guzzle-log-middleware

1. Go to this page and download the library: Download malvik-lab/guzzle-log-middleware library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

malvik-lab / guzzle-log-middleware example snippets


// composer new \Laminas\Log\Writer\Stream('/path/to/log/file.log');
$laminasLogLogger = new \Laminas\Log\Logger();
$laminasLogLogger->addWriter($writer);
$log = new \Laminas\Log\PsrLoggerAdapter($laminasLogLogger);
$adapter = [
    'adapter' => $log,
    'options' => [
        'template' => \GuzzleHttp\MessageFormatter::CLF, // Not mandatory. For more information: https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php
    ]
];

// composer  \Laminas\Cache\StorageFactory::factory([
    'adapter' => [
        'name' => 'redis',
        'options' => [
            'namespace' => '',
            'namespace_separator' => '_',
            'ttl' => 3600,
            'password' => 'my-redis-password',
            'server' => [
                'host' => 'my-redis-host',
                'port' => 6379,
            ],
        ],
    ],
    'plugins' => [
        'serializer',
    ],
]);
$cache = new \Laminas\Cache\Psr\CacheItemPool\CacheItemPoolDecorator($storage);
$adapter = [
    'adapter' => $cache,
    'options' => [
        'keyPrefix' => 'my-redis-key-prefix', // Not mandatory
        'template' => \GuzzleHttp\MessageFormatter::CLF, // Not mandatory. For more information: https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php
    ]
];

$adapter = [
    'adapter' => 'filesystem',
    'options' => [
        'dirPath' => '/path/to/log/folder',
        'template' => \GuzzleHttp\MessageFormatter::CLF, // Not mandatory. For more information: https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php
    ]
];

$adapter = [
    'adapter' => 'filesystem',
    'options' => [
        'filePath' => '/path/to/log/file.log',
        'template' => \GuzzleHttp\MessageFormatter::CLF, // Not mandatory. For more information: https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php
    ]
];

// In the constructor you can inject one or more adapters

$guzzleLogMiddleware = new \MalvikLab\GuzzleLogMiddleware\GuzzleLogMiddleware([
    $adapter
]);

$stack = \GuzzleHttp\HandlerStack::create();
$stack->push($guzzleLogMiddleware);

// your client
$client = new \GuzzleHttp\Client([
    'handler' => $stack
]);