php-http / logger-plugin

PSR-3 Logger plugin for HTTPlug
http://docs.php-http.org/en/latest/plugins/logger.html
MIT License
282 stars 7 forks source link

Deduplicate request logs, couple request<>response by UID #19

Closed ro0NL closed 2 years ago

ro0NL commented 2 years ago
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Related tickets fixes #18
Documentation if this is a new feature, link to pull request in https://github.com/php-http/documentation that adds relevant documentation
License MIT

Given

httplug:
    clients:
        test:
            service: 'psr18.http_client'
            plugins:
                - httplug.plugin.logger
        test_with_error_handler:
            service: 'psr18.http_client'
            plugins:
                - httplug.plugin.logger
                - httplug.plugin.error
$request = $this->requestFactory->createRequest('GET', 'https://httpbin.org/status/200');
$requestError = $this->requestFactory->createRequest('GET', 'https://httpbin.org/status/500');

$this->test->sendRequest($request);
$this->test->sendRequest($requestError);
$this->testWithErrorHandler->sendRequest($requestError);
18:54:46 INFO      [httplug] Sending request:
GET https://httpbin.org/status/200 1.1 ["request" => Nyholm\Psr7\Request { …},"uid" => "61fd6866755f45.27019150"]
18:54:46 INFO      [httplug] Received response:
200 OK 1.1 ["request" => Nyholm\Psr7\Request { …},"response" => Nyholm\Psr7\Response { …},"milliseconds" => 490,"uid" => "61fd6866755f45.27019150"]

18:54:46 INFO      [httplug] Sending request:
GET https://httpbin.org/status/500 1.1 ["request" => Nyholm\Psr7\Request { …},"uid" => "61fd6866ed29b5.48079388"]
18:54:47 INFO      [httplug] Received response:
500 Internal Server Error 1.1 ["request" => Nyholm\Psr7\Request { …},"response" => Nyholm\Psr7\Response { …},"milliseconds" => 129,"uid" => "61fd6866ed29b5.48079388"]

18:54:47 INFO      [httplug] Sending request:
GET https://httpbin.org/status/500 1.1 ["request" => Nyholm\Psr7\Request { …},"uid" => "61fd686718a270.30706898"]
18:54:47 ERROR     [httplug] Error:
Internal Server Error
with response:
500 Internal Server Error 1.1 ["request" => Nyholm\Psr7\Request { …},"response" => Nyholm\Psr7\Response { …},"exception" => Http\Client\Common\Exception\ServerErrorException { …},"milliseconds" => 107,"uid" => "61fd686718a270.30706898"]

For me this works OK, and could go as a patch release. But any next version works for us :+1:

dbu commented 2 years ago

we should also update https://github.com/php-http/documentation/blob/master/plugins/logger.rst to have the example use a fingers crossed logger and a note why we use that instead of a straight logger.

ro0NL commented 2 years ago

@dbu done. I'll let you update docs, i dont see how "fingerscrossed" is relevant. We always log certain channels, that are used dedicated for http traffic.

dbu commented 2 years ago

thanks a lot!

i addjusted the changelog and squashed commits, then merged locally.

dbu commented 2 years ago

i wait with tagging until #16 is clarified and maybe https://github.com/php-http/message/pull/146 as well