Skip to content

wearesho-team/yii2-guzzle

Repository files navigation

Yii2 Guzzle http log

Test & Lint Latest Stable Version Total Downloads codecov

Library for storing http queries into database

Installation

composer require wearesho-team/yii2-guzzle:^2.0

Usage

  1. Append Bootstrap to your application
<?php

use Wearesho\Yii\Guzzle;
use Psr\Http\Message\RequestInterface;

return [
    'bootstrap' => [
        'http-log' => [
            'class' => Guzzle\Bootstrap::class,
            // Logs exclude rules
            'exclude' => [
                // url regular expression
                '/^.*(google).*$/iu',
                // or closure (return true if you don't need to log request)
                fn(Message\RequestInterface $request): bool => $request->getUri()->getHost() === 'zsu.gov.ua/'
            ],
            // Guzzle client configuration settings
            'config' => [
                'timeout' => 10,
            ],
        ],
    ],
];
  1. Use \Yii::$container to instantiate GuzzleHttp\Client and send requests to log them
  2. Use Guzzle\Log\Request, Guzzle\Log\Response, Guzzle\Log\Exception to find logs

Note: for not UTF-8 request or response body (for example, files) (invalid UTF-8 bytes) will be saved.

Log using Queue

Sometimes it is not possible to use synchronous saving of query logs when the query was executed during an initiated database transaction. In this case it is possible that the transaction will not be saved and the logs will be lost.

The recommended solution is to use QueueRepository to save the logs. To do this, configure Bootstrap this way:

<?php

use Wearesho\Yii\Guzzle;
use Psr\Http\Message\RequestInterface;

return [
    'bootstrap' => [
        'http-log' => [
            'class' => Guzzle\Bootstrap::class,
            // Here you configure repository
            'repository' => [
                'class' => Guzzle\Log\QueueRepository::class,
            ],
            // Logs exclude rules
            'exclude' => [
                // your rules
            ],
            // Guzzle client configuration settings
            'config' => [
                'timeout' => 10,
            ],
        ],
    ],
];

Also, you have the option to implement your own RepositoryInterface and use it.

TODO

  • Tests for Job

Contributors

License

MIT

About

Container configuration with requests logs for Yii2

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages