Skip to content

Commit 01fa444

Browse files
committed
Merge branch 'log-context'
2 parents 27430f5 + 8d51405 commit 01fa444

File tree

3 files changed

+15
-29
lines changed

3 files changed

+15
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
If you use a logger that does not log `info` severity and want the request
88
logged when an error happened, use a Fingerscrossed log handler to also log
99
info if any error is logged.
10+
- Removed the request and response from the log context. They did not get
11+
printed because they don't implement `__toString`.
1012

1113
## 1.2.2 - 2021-07-26
1214

spec/LoggerPluginSpec.php

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,16 @@ function it_logs_request_and_response(
4545
$logger->info(
4646
"Sending request:\nGET / 1.1",
4747
Argument::that(
48-
function(array $context) use ($request) {
49-
return $context['request'] === $request->getWrappedObject()
50-
&& is_string($context['uid'])
51-
;
48+
function(array $context) {
49+
return is_string($context['uid']);
5250
}
5351
)
5452
)->shouldBeCalled();
5553
$logger->info(
5654
"Received response:\n200 OK 1.1",
5755
Argument::that(
58-
function(array $context) use ($request, $response) {
59-
return $context['request'] === $request->getWrappedObject()
60-
&& $context['response'] === $response->getWrappedObject()
61-
&& is_int($context['milliseconds'])
56+
function(array $context) {
57+
return is_int($context['milliseconds'])
6258
&& is_string($context['uid'])
6359
;
6460
}
@@ -81,19 +77,16 @@ function it_logs_exception(LoggerInterface $logger, Formatter $formatter, Reques
8177
$logger->info(
8278
"Sending request:\nGET / 1.1",
8379
Argument::that(
84-
function(array $context) use ($request) {
85-
return $context['request'] === $request->getWrappedObject()
86-
&& is_string($context['uid'])
87-
;
80+
function(array $context) {
81+
return is_string($context['uid']);
8882
}
8983
)
9084
)->shouldBeCalled();
9185
$logger->error(
9286
"Error:\nCannot connect\nwhen sending request:\nGET / 1.1",
9387
Argument::that(
94-
function(array $context) use ($request, $exception) {
95-
return $context['request'] === $request->getWrappedObject()
96-
&& $context['exception'] === $exception
88+
function(array $context) use ($exception) {
89+
return $context['exception'] === $exception
9790
&& is_int($context['milliseconds'])
9891
&& is_string($context['uid'])
9992
;
@@ -122,20 +115,16 @@ function it_logs_response_within_exception(
122115
$logger->info(
123116
"Sending request:\nGET / 1.1",
124117
Argument::that(
125-
function(array $context) use ($request) {
126-
return $context['request'] === $request->getWrappedObject()
127-
&& is_string($context['uid'])
128-
;
118+
function(array $context) {
119+
return is_string($context['uid']);
129120
}
130121
)
131122
)->shouldBeCalled();
132123
$logger->error(
133124
"Error:\nForbidden\nwith response:\n403 Forbidden 1.1",
134125
Argument::that(
135-
function(array $context) use ($request, $response, $exception) {
136-
return $context['request'] === $request->getWrappedObject()
137-
&& $context['response'] === $response->getWrappedObject()
138-
&& $context['exception'] === $exception
126+
function(array $context) use ($exception) {
127+
return $context['exception'] === $exception
139128
&& is_int($context['milliseconds'])
140129
&& is_string($context['uid'])
141130
;

src/LoggerPlugin.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,11 @@ 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 ($request, $start, $uid) {
38+
return $next($request)->then(function (ResponseInterface $response) use ($start, $uid) {
3939
$milliseconds = (int) round(hrtime(true) / 1E6 - $start);
4040
$this->logger->info(
4141
sprintf("Received response:\n%s", $this->formatter->formatResponse($response)),
4242
[
43-
'request' => $request,
44-
'response' => $response,
4543
'milliseconds' => $milliseconds,
4644
'uid' => $uid,
4745
]
@@ -54,8 +52,6 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
5452
$this->logger->error(
5553
sprintf("Error:\n%s\nwith response:\n%s", $exception->getMessage(), $this->formatter->formatResponse($exception->getResponse())),
5654
[
57-
'request' => $request,
58-
'response' => $exception->getResponse(),
5955
'exception' => $exception,
6056
'milliseconds' => $milliseconds,
6157
'uid' => $uid,
@@ -65,7 +61,6 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
6561
$this->logger->error(
6662
sprintf("Error:\n%s\nwhen sending request:\n%s", $exception->getMessage(), $this->formatter->formatRequest($request)),
6763
[
68-
'request' => $request,
6964
'exception' => $exception,
7065
'milliseconds' => $milliseconds,
7166
'uid' => $uid,

0 commit comments

Comments
 (0)