Skip to content

Commit 262954e

Browse files
authored
Merge pull request #5 from php-http/time
Adding time for the request
2 parents 22687b8 + b6231cd commit 262954e

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ php:
1313
- 7.0
1414
- 7.1
1515
- 7.2
16-
- hhvm
1716

1817
env:
1918
global:

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Change Log
22

33

4+
## 1.1.0 - 2018-04-29
5+
6+
- Start measuring the time it took to do the request.
7+
48
## 1.0.0 - 2016-05-05
59

610
- Initial release

spec/LoggerPluginSpec.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Http\Promise\FulfilledPromise;
88
use Http\Promise\RejectedPromise;
99
use Http\Message\Formatter;
10+
use Prophecy\Argument;
1011
use Psr\Http\Message\RequestInterface;
1112
use Psr\Http\Message\ResponseInterface;
1213
use Psr\Http\Message\UriInterface;
@@ -40,7 +41,17 @@ function it_logs_request_and_response(
4041
$formatter->formatResponse($response)->willReturn('200 OK 1.1');
4142

4243
$logger->info("Sending request:\nGET / 1.1", ['request' => $request])->shouldBeCalled();
43-
$logger->info("Received response:\n200 OK 1.1\n\nfor request:\nGET / 1.1", ['request' => $request, 'response' => $response])->shouldBeCalled();
44+
$logger->info(
45+
"Received response:\n200 OK 1.1\n\nfor request:\nGET / 1.1",
46+
Argument::that(
47+
function(array $context) use ($request, $response) {
48+
return $context['request'] === $request->getWrappedObject()
49+
&& $context['response'] === $response->getWrappedObject()
50+
&& is_int($context['milliseconds'])
51+
;
52+
}
53+
)
54+
)->shouldBeCalled();
4455

4556
$next = function () use ($response) {
4657
return new FulfilledPromise($response->getWrappedObject());
@@ -56,7 +67,17 @@ function it_logs_exception(LoggerInterface $logger, Formatter $formatter, Reques
5667
$exception = new NetworkException('Cannot connect', $request->getWrappedObject());
5768

5869
$logger->info("Sending request:\nGET / 1.1", ['request' => $request])->shouldBeCalled();
59-
$logger->error("Error:\nCannot connect\nwhen sending request:\nGET / 1.1", ['request' => $request, 'exception' => $exception])->shouldBeCalled();
70+
$logger->error(
71+
"Error:\nCannot connect\nwhen sending request:\nGET / 1.1",
72+
Argument::that(
73+
function(array $context) use ($request, $exception) {
74+
return $context['request'] === $request->getWrappedObject()
75+
&& $context['exception'] === $exception
76+
&& is_int($context['milliseconds'])
77+
;
78+
}
79+
)
80+
)->shouldBeCalled();
6081

6182
$next = function () use ($exception) {
6283
return new RejectedPromise($exception);
@@ -77,11 +98,18 @@ function it_logs_response_within_exception(
7798
$exception = new HttpException('Forbidden', $request->getWrappedObject(), $response->getWrappedObject());
7899

79100
$logger->info("Sending request:\nGET / 1.1", ['request' => $request])->shouldBeCalled();
80-
$logger->error("Error:\nForbidden\nwith response:\n403 Forbidden 1.1\n\nwhen sending request:\nGET / 1.1", [
81-
'request' => $request,
82-
'response' => $response,
83-
'exception' => $exception
84-
])->shouldBeCalled();
101+
$logger->error(
102+
"Error:\nForbidden\nwith response:\n403 Forbidden 1.1\n\nwhen sending request:\nGET / 1.1",
103+
Argument::that(
104+
function(array $context) use ($request, $response, $exception) {
105+
return $context['request'] === $request->getWrappedObject()
106+
&& $context['response'] === $response->getWrappedObject()
107+
&& $context['exception'] === $exception
108+
&& is_int($context['milliseconds'])
109+
;
110+
}
111+
)
112+
)->shouldBeCalled();
85113

86114
$next = function () use ($exception) {
87115
return new RejectedPromise($exception);

src/LoggerPlugin.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,31 @@ public function __construct(LoggerInterface $logger, Formatter $formatter = null
3232
*/
3333
public function handleRequest(RequestInterface $request, callable $next, callable $first)
3434
{
35+
$start = microtime(true);
3536
$this->logger->info(sprintf("Sending request:\n%s", $this->formatter->formatRequest($request)), ['request' => $request]);
3637

37-
return $next($request)->then(function (ResponseInterface $response) use ($request) {
38+
return $next($request)->then(function (ResponseInterface $response) use ($request, $start) {
39+
$milliseconds = (int) round((microtime(true) - $start) * 1000);
3840
$this->logger->info(
3941
sprintf("Received response:\n%s\n\nfor request:\n%s", $this->formatter->formatResponse($response), $this->formatter->formatRequest($request)),
4042
[
4143
'request' => $request,
4244
'response' => $response,
45+
'milliseconds' => $milliseconds,
4346
]
4447
);
4548

4649
return $response;
47-
}, function (Exception $exception) use ($request) {
50+
}, function (Exception $exception) use ($request, $start) {
51+
$milliseconds = (int) round((microtime(true) - $start) * 1000);
4852
if ($exception instanceof Exception\HttpException) {
4953
$this->logger->error(
5054
sprintf("Error:\n%s\nwith response:\n%s\n\nwhen sending request:\n%s", $exception->getMessage(), $this->formatter->formatResponse($exception->getResponse()), $this->formatter->formatRequest($request)),
5155
[
5256
'request' => $request,
5357
'response' => $exception->getResponse(),
5458
'exception' => $exception,
59+
'milliseconds' => $milliseconds,
5560
]
5661
);
5762
} else {
@@ -60,6 +65,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
6065
[
6166
'request' => $request,
6267
'exception' => $exception,
68+
'milliseconds' => $milliseconds,
6369
]
6470
);
6571
}

0 commit comments

Comments
 (0)