Skip to content

Commit 27430f5

Browse files
committed
Merge branch 'patch-1'
2 parents 665b13e + 631e8db commit 27430f5

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Change Log
22

3+
## 1.3.0 - unreleased
4+
5+
- Do not log request when loggin response again, but use UID to identify request
6+
that belongs to response.
7+
If you use a logger that does not log `info` severity and want the request
8+
logged when an error happened, use a Fingerscrossed log handler to also log
9+
info if any error is logged.
10+
311
## 1.2.2 - 2021-07-26
412

513
- Allow installation with psr/log version 2 and 3

spec/LoggerPluginSpec.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,24 @@ function it_logs_request_and_response(
4242
$formatter->formatRequest($request)->willReturn('GET / 1.1');
4343
$formatter->formatResponse($response)->willReturn('200 OK 1.1');
4444

45-
$logger->info("Sending request:\nGET / 1.1", ['request' => $request])->shouldBeCalled();
4645
$logger->info(
47-
"Received response:\n200 OK 1.1\n\nfor request:\nGET / 1.1",
46+
"Sending request:\nGET / 1.1",
47+
Argument::that(
48+
function(array $context) use ($request) {
49+
return $context['request'] === $request->getWrappedObject()
50+
&& is_string($context['uid'])
51+
;
52+
}
53+
)
54+
)->shouldBeCalled();
55+
$logger->info(
56+
"Received response:\n200 OK 1.1",
4857
Argument::that(
4958
function(array $context) use ($request, $response) {
5059
return $context['request'] === $request->getWrappedObject()
5160
&& $context['response'] === $response->getWrappedObject()
5261
&& is_int($context['milliseconds'])
62+
&& is_string($context['uid'])
5363
;
5464
}
5565
)
@@ -68,14 +78,24 @@ function it_logs_exception(LoggerInterface $logger, Formatter $formatter, Reques
6878

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

71-
$logger->info("Sending request:\nGET / 1.1", ['request' => $request])->shouldBeCalled();
81+
$logger->info(
82+
"Sending request:\nGET / 1.1",
83+
Argument::that(
84+
function(array $context) use ($request) {
85+
return $context['request'] === $request->getWrappedObject()
86+
&& is_string($context['uid'])
87+
;
88+
}
89+
)
90+
)->shouldBeCalled();
7291
$logger->error(
7392
"Error:\nCannot connect\nwhen sending request:\nGET / 1.1",
7493
Argument::that(
7594
function(array $context) use ($request, $exception) {
7695
return $context['request'] === $request->getWrappedObject()
7796
&& $context['exception'] === $exception
7897
&& is_int($context['milliseconds'])
98+
&& is_string($context['uid'])
7999
;
80100
}
81101
)
@@ -99,15 +119,25 @@ function it_logs_response_within_exception(
99119

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

102-
$logger->info("Sending request:\nGET / 1.1", ['request' => $request])->shouldBeCalled();
122+
$logger->info(
123+
"Sending request:\nGET / 1.1",
124+
Argument::that(
125+
function(array $context) use ($request) {
126+
return $context['request'] === $request->getWrappedObject()
127+
&& is_string($context['uid'])
128+
;
129+
}
130+
)
131+
)->shouldBeCalled();
103132
$logger->error(
104-
"Error:\nForbidden\nwith response:\n403 Forbidden 1.1\n\nwhen sending request:\nGET / 1.1",
133+
"Error:\nForbidden\nwith response:\n403 Forbidden 1.1",
105134
Argument::that(
106135
function(array $context) use ($request, $response, $exception) {
107136
return $context['request'] === $request->getWrappedObject()
108137
&& $context['response'] === $response->getWrappedObject()
109138
&& $context['exception'] === $exception
110139
&& is_int($context['milliseconds'])
140+
&& is_string($context['uid'])
111141
;
112142
}
113143
)

src/LoggerPlugin.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,33 @@ public function __construct(LoggerInterface $logger, Formatter $formatter = null
3232
protected function doHandleRequest(RequestInterface $request, callable $next, callable $first)
3333
{
3434
$start = hrtime(true) / 1E6;
35-
$this->logger->info(sprintf("Sending request:\n%s", $this->formatter->formatRequest($request)), ['request' => $request]);
35+
$uid = uniqid('', true);
36+
$this->logger->info(sprintf("Sending request:\n%s", $this->formatter->formatRequest($request)), ['request' => $request, 'uid' => $uid]);
3637

37-
return $next($request)->then(function (ResponseInterface $response) use ($request, $start) {
38+
return $next($request)->then(function (ResponseInterface $response) use ($request, $start, $uid) {
3839
$milliseconds = (int) round(hrtime(true) / 1E6 - $start);
3940
$this->logger->info(
40-
sprintf("Received response:\n%s\n\nfor request:\n%s", $this->formatter->formatResponse($response), $this->formatter->formatRequest($request)),
41+
sprintf("Received response:\n%s", $this->formatter->formatResponse($response)),
4142
[
4243
'request' => $request,
4344
'response' => $response,
4445
'milliseconds' => $milliseconds,
46+
'uid' => $uid,
4547
]
4648
);
4749

4850
return $response;
49-
}, function (Exception $exception) use ($request, $start) {
51+
}, function (Exception $exception) use ($request, $start, $uid) {
5052
$milliseconds = (int) round((hrtime(true) / 1E6 - $start));
5153
if ($exception instanceof Exception\HttpException) {
5254
$this->logger->error(
53-
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)),
55+
sprintf("Error:\n%s\nwith response:\n%s", $exception->getMessage(), $this->formatter->formatResponse($exception->getResponse())),
5456
[
5557
'request' => $request,
5658
'response' => $exception->getResponse(),
5759
'exception' => $exception,
5860
'milliseconds' => $milliseconds,
61+
'uid' => $uid,
5962
]
6063
);
6164
} else {
@@ -65,6 +68,7 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
6568
'request' => $request,
6669
'exception' => $exception,
6770
'milliseconds' => $milliseconds,
71+
'uid' => $uid,
6872
]
6973
);
7074
}

0 commit comments

Comments
 (0)