Skip to content

Commit 9aeb868

Browse files
authored
Merge pull request #21 from ro0NL/[email protected]
support [email protected]
2 parents 01fa444 + d40ccee commit 9aeb868

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

spec/LoggerPluginSpec.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ function it_logs_request_and_response(
4040
ResponseInterface $response
4141
) {
4242
$formatter->formatRequest($request)->willReturn('GET / 1.1');
43-
$formatter->formatResponse($response)->willReturn('200 OK 1.1');
43+
if (method_exists(Formatter\SimpleFormatter::class, 'formatResponseForRequest')) {
44+
$formatter->formatResponseForRequest($response, $request)->willReturn('200 OK 1.1');
45+
} else {
46+
$formatter->formatResponse($response)->willReturn('200 OK 1.1');
47+
}
4448

4549
$logger->info(
4650
"Sending request:\nGET / 1.1",
@@ -108,7 +112,11 @@ function it_logs_response_within_exception(
108112
ResponseInterface $response
109113
) {
110114
$formatter->formatRequest($request)->willReturn('GET / 1.1');
111-
$formatter->formatResponse($response)->willReturn('403 Forbidden 1.1');
115+
if (method_exists(Formatter\SimpleFormatter::class, 'formatResponseForRequest')) {
116+
$formatter->formatResponseForRequest($response, $request)->willReturn('403 Forbidden 1.1');
117+
} else {
118+
$formatter->formatResponse($response)->willReturn('403 Forbidden 1.1');
119+
}
112120

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

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)