Skip to content

Commit a9805cf

Browse files
committed
1 parent 01fa444 commit a9805cf

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

spec/LoggerPluginSpec.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ function it_logs_request_and_response(
4141
) {
4242
$formatter->formatRequest($request)->willReturn('GET / 1.1');
4343
$formatter->formatResponse($response)->willReturn('200 OK 1.1');
44+
$formatter->formatResponseForRequest($response, $request)->willReturn('200 OK 1.1');
4445

4546
$logger->info(
4647
"Sending request:\nGET / 1.1",
@@ -109,6 +110,7 @@ function it_logs_response_within_exception(
109110
) {
110111
$formatter->formatRequest($request)->willReturn('GET / 1.1');
111112
$formatter->formatResponse($response)->willReturn('403 Forbidden 1.1');
113+
$formatter->formatResponseForRequest($response, $request)->willReturn('403 Forbidden 1.1');
112114

113115
$exception = new HttpException('Forbidden', $request->getWrappedObject(), $response->getWrappedObject());
114116

src/LoggerPlugin.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
3535
$uid = uniqid('', true);
3636
$this->logger->info(sprintf("Sending request:\n%s", $this->formatter->formatRequest($request)), ['request' => $request, 'uid' => $uid]);
3737

38-
return $next($request)->then(function (ResponseInterface $response) use ($start, $uid) {
38+
return $next($request)->then(function (ResponseInterface $response) use ($start, $uid, $request) {
3939
$milliseconds = (int) round(hrtime(true) / 1E6 - $start);
40+
$formattedResponse = method_exists($this->formatter, 'formatResponseForRequest')
41+
? $this->formatter->formatResponseForRequest($response, $request)
42+
: $this->formatter->formatResponse($response);
4043
$this->logger->info(
41-
sprintf("Received response:\n%s", $this->formatter->formatResponse($response)),
44+
sprintf("Received response:\n%s", $formattedResponse),
4245
[
4346
'milliseconds' => $milliseconds,
4447
'uid' => $uid,
@@ -49,8 +52,11 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
4952
}, function (Exception $exception) use ($request, $start, $uid) {
5053
$milliseconds = (int) round((hrtime(true) / 1E6 - $start));
5154
if ($exception instanceof Exception\HttpException) {
55+
$formattedResponse = method_exists($this->formatter, 'formatResponseForRequest')
56+
? $this->formatter->formatResponseForRequest($exception->getResponse(), $exception->getRequest())
57+
: $this->formatter->formatResponse($exception->getResponse());
5258
$this->logger->error(
53-
sprintf("Error:\n%s\nwith response:\n%s", $exception->getMessage(), $this->formatter->formatResponse($exception->getResponse())),
59+
sprintf("Error:\n%s\nwith response:\n%s", $exception->getMessage(), $formattedResponse),
5460
[
5561
'exception' => $exception,
5662
'milliseconds' => $milliseconds,

0 commit comments

Comments
 (0)