Skip to content

Adding time for the request #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ php:
- 7.0
- 7.1
- 7.2
- hhvm

env:
global:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log


## 1.1.0 - 2018-03-29

- Start measuring the time it took to do the request.

## 1.0.0 - 2016-05-05

- Initial release
42 changes: 35 additions & 7 deletions spec/LoggerPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Http\Promise\FulfilledPromise;
use Http\Promise\RejectedPromise;
use Http\Message\Formatter;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
Expand Down Expand Up @@ -40,7 +41,17 @@ function it_logs_request_and_response(
$formatter->formatResponse($response)->willReturn('200 OK 1.1');

$logger->info("Sending request:\nGET / 1.1", ['request' => $request])->shouldBeCalled();
$logger->info("Received response:\n200 OK 1.1\n\nfor request:\nGET / 1.1", ['request' => $request, 'response' => $response])->shouldBeCalled();
$logger->info(
"Received response:\n200 OK 1.1\n\nfor request:\nGET / 1.1",
Argument::that(
function(array $context) use ($request, $response) {
return $context['request'] === $request->getWrappedObject()
&& $context['response'] === $response->getWrappedObject()
&& is_int($context['milliseconds'])
;
}
)
)->shouldBeCalled();

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

$logger->info("Sending request:\nGET / 1.1", ['request' => $request])->shouldBeCalled();
$logger->error("Error:\nCannot connect\nwhen sending request:\nGET / 1.1", ['request' => $request, 'exception' => $exception])->shouldBeCalled();
$logger->error(
"Error:\nCannot connect\nwhen sending request:\nGET / 1.1",
Argument::that(
function(array $context) use ($request, $exception) {
return $context['request'] === $request->getWrappedObject()
&& $context['exception'] === $exception
&& is_int($context['milliseconds'])
;
}
)
)->shouldBeCalled();

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

$logger->info("Sending request:\nGET / 1.1", ['request' => $request])->shouldBeCalled();
$logger->error("Error:\nForbidden\nwith response:\n403 Forbidden 1.1\n\nwhen sending request:\nGET / 1.1", [
'request' => $request,
'response' => $response,
'exception' => $exception
])->shouldBeCalled();
$logger->error(
"Error:\nForbidden\nwith response:\n403 Forbidden 1.1\n\nwhen sending request:\nGET / 1.1",
Argument::that(
function(array $context) use ($request, $response, $exception) {
return $context['request'] === $request->getWrappedObject()
&& $context['response'] === $response->getWrappedObject()
&& $context['exception'] === $exception
&& is_int($context['milliseconds'])
;
}
)
)->shouldBeCalled();

$next = function () use ($exception) {
return new RejectedPromise($exception);
Expand Down
10 changes: 8 additions & 2 deletions src/LoggerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,31 @@ public function __construct(LoggerInterface $logger, Formatter $formatter = null
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
{
$start = microtime(true);
$this->logger->info(sprintf("Sending request:\n%s", $this->formatter->formatRequest($request)), ['request' => $request]);

return $next($request)->then(function (ResponseInterface $response) use ($request) {
return $next($request)->then(function (ResponseInterface $response) use ($request, $start) {
$milliseconds = (int) round((microtime(true) - $start) * 1000);
$this->logger->info(
sprintf("Received response:\n%s\n\nfor request:\n%s", $this->formatter->formatResponse($response), $this->formatter->formatRequest($request)),
[
'request' => $request,
'response' => $response,
'milliseconds' => $milliseconds,
]
);

return $response;
}, function (Exception $exception) use ($request) {
}, function (Exception $exception) use ($request, $start) {
$milliseconds = (int) round((microtime(true) - $start) * 1000);
if ($exception instanceof Exception\HttpException) {
$this->logger->error(
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)),
[
'request' => $request,
'response' => $exception->getResponse(),
'exception' => $exception,
'milliseconds' => $milliseconds,
]
);
} else {
Expand All @@ -60,6 +65,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
[
'request' => $request,
'exception' => $exception,
'milliseconds' => $milliseconds,
]
);
}
Expand Down