Skip to content

Commit 6f40e9a

Browse files
committed
Adding time for the request
1 parent 22687b8 commit 6f40e9a

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

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-03-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: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ function it_logs_request_and_response(
3434
LoggerInterface $logger,
3535
Formatter $formatter,
3636
RequestInterface $request,
37-
ResponseInterface $response
37+
ResponseInterface $response,
38+
$milliseconds
3839
) {
3940
$formatter->formatRequest($request)->willReturn('GET / 1.1');
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("Received response:\n200 OK 1.1\n\nfor request:\nGET / 1.1", ['request' => $request, 'response' => $response, 'milliseconds' => $milliseconds])->shouldBeCalled();
4445

4546
$next = function () use ($response) {
4647
return new FulfilledPromise($response->getWrappedObject());
@@ -49,14 +50,14 @@ function it_logs_request_and_response(
4950
$this->handleRequest($request, $next, function () {});
5051
}
5152

52-
function it_logs_exception(LoggerInterface $logger, Formatter $formatter, RequestInterface $request)
53+
function it_logs_exception(LoggerInterface $logger, Formatter $formatter, RequestInterface $request, $milliseconds)
5354
{
5455
$formatter->formatRequest($request)->willReturn('GET / 1.1');
5556

5657
$exception = new NetworkException('Cannot connect', $request->getWrappedObject());
5758

5859
$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();
60+
$logger->error("Error:\nCannot connect\nwhen sending request:\nGET / 1.1", ['request' => $request, 'exception' => $exception, 'milliseconds' => $milliseconds])->shouldBeCalled();
6061

6162
$next = function () use ($exception) {
6263
return new RejectedPromise($exception);
@@ -69,7 +70,8 @@ function it_logs_response_within_exception(
6970
LoggerInterface $logger,
7071
Formatter $formatter,
7172
RequestInterface $request,
72-
ResponseInterface $response
73+
ResponseInterface $response,
74+
$milliseconds
7375
) {
7476
$formatter->formatRequest($request)->willReturn('GET / 1.1');
7577
$formatter->formatResponse($response)->willReturn('403 Forbidden 1.1');
@@ -78,9 +80,10 @@ function it_logs_response_within_exception(
7880

7981
$logger->info("Sending request:\nGET / 1.1", ['request' => $request])->shouldBeCalled();
8082
$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
83+
'request' => $request,
84+
'response' => $response,
85+
'exception' => $exception,
86+
'milliseconds' => $milliseconds
8487
])->shouldBeCalled();
8588

8689
$next = function () use ($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();
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 = round(microtime() - $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 = round(microtime() - $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)