Skip to content

Commit

Permalink
Merge pull request #22 from Mapudo/MAP-2890
Browse files Browse the repository at this point in the history
MAP-2890
  • Loading branch information
kobelobster authored Nov 29, 2018
2 parents 10be6d9 + 1055538 commit 20fade1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All Notable changes to `mapudo/guzzle-bundle` will be documented in this file.

Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## [2.2.0] - 2018-11-29
### Changed
- Add `duration` property to be logged in `LogMiddleware` to keep track of the request duration

## [2.1.1] - 2018-01-11
### Changed
- Changed the template include paths
Expand Down
41 changes: 29 additions & 12 deletions src/Middleware/LogMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\MessageFormatter;
use GuzzleHttp\TransferStats;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -56,25 +57,22 @@ public function log(): \Closure

return function (callable $handler) use ($logger, $formatter) {
return function (RequestInterface $request, array $options) use ($handler, $logger, $formatter) {
$duration = null;
$options['on_stats'] = function (TransferStats $stats) use (&$duration) {
$duration = $stats->getTransferTime();
};

return $handler($request, $options)->then(
function (ResponseInterface $response) use ($logger, $request, $formatter) {
function (ResponseInterface $response) use ($logger, $request, $formatter, $duration) {
$message = $formatter->format($request, $response);
$request->getBody()->rewind();
$context = [
'request' => $this->normalizer->normalize($request),
'response' => $this->normalizer->normalize($response),
'client' => $this->clientName,
];
$logger->info($message, $context);
$logger->info($message, $this->buildContext($request, $response, $duration));

return $response;
},
function ($reason) use ($logger, $request, $formatter) {
function ($reason) use ($logger, $request, $formatter, $duration) {
$response = $reason instanceof RequestException ? $reason->getResponse() : null;
$message = $formatter->format($request, $response, $reason);
$context = compact('request', 'response');

$logger->notice($message, $context);
$logger->error($message, $this->buildContext($request, $response, $duration));

return \GuzzleHttp\Promise\rejection_for($reason);
}
Expand All @@ -94,4 +92,23 @@ public function setClientName(string $clientName): LogMiddleware
$this->clientName = $clientName;
return $this;
}

private function buildContext(
RequestInterface $request,
ResponseInterface $response = null,
float $duration = null
): array {
$request->getBody()->rewind();
$context = [
'request' => $this->normalizer->normalize($request),
'client' => $this->clientName,
'duration' => $duration,
];

if ($response !== null) {
$context['response'] = $this->normalizer->normalize($response);
}

return $context;
}
}

0 comments on commit 20fade1

Please sign in to comment.