Skip to content

Commit cfff2d7

Browse files
committed
Make it possible to not log on post but log on error only
1 parent b2136d6 commit cfff2d7

File tree

1 file changed

+58
-38
lines changed

1 file changed

+58
-38
lines changed

src/LoggerMiddleware.php

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,7 @@ class LoggerMiddleware implements MiddlewareInterface
2626
/**
2727
* @var array
2828
*/
29-
private $context = [
30-
self::REQUEST => [
31-
'method' => null,
32-
'uri' => null,
33-
'protocol_version' => null,
34-
'headers' => [],
35-
],
36-
self::RESPONSE => [
37-
'status_code' => null,
38-
'status_reason' => null,
39-
'protocol_version' => null,
40-
'headers' => [],
41-
],
42-
];
29+
private $context = [];
4330

4431
/**
4532
* LogMiddleware constructor.
@@ -62,15 +49,20 @@ public function pre(
6249
string $transactionId,
6350
array $options = []
6451
): CancellablePromiseInterface {
65-
if (!isset($options[self::class][Options::LEVEL])) {
52+
if (!isset($options[self::class][Options::LEVEL]) && !isset($options[self::class][Options::ERROR_LEVEL])) {
6653
return resolve($request);
6754
}
6855

6956
$this->context[$transactionId][self::REQUEST]['method'] = $request->getMethod();
7057
$this->context[$transactionId][self::REQUEST]['uri'] = (string)$request->getUri();
7158
$this->context[$transactionId][self::REQUEST]['protocol_version'] = (string)$request->getProtocolVersion();
7259
$ignoreHeaders = $options[self::class][Options::IGNORE_HEADERS] ?? [];
73-
$this->iterateHeaders(self::REQUEST, $transactionId, $request->getHeaders(), $ignoreHeaders);
60+
$this->context[$transactionId] = $this->iterateHeaders(
61+
$this->context[$transactionId],
62+
self::REQUEST,
63+
$request->getHeaders(),
64+
$ignoreHeaders
65+
);
7466

7567
return resolve($request);
7668
}
@@ -87,17 +79,24 @@ public function post(
8779
string $transactionId,
8880
array $options = []
8981
): CancellablePromiseInterface {
90-
if (!isset($options[self::class][Options::LEVEL])) {
82+
if (!isset($this->context[$transactionId])) {
83+
return resolve($response);
84+
}
85+
86+
$context = $this->context[$transactionId];
87+
if (!isset($options[self::class][Options::LEVEL]) && !isset($options[self::class][Options::ERROR_LEVEL])) {
9188
unset($this->context[$transactionId]);
89+
}
90+
91+
if (!isset($options[self::class][Options::LEVEL])) {
9292
return resolve($response);
9393
}
9494

9595
$message = 'Request ' . $transactionId . ' completed.';
9696

97-
$this->addResponseToContext($response, $transactionId, $options);
97+
$context = $this->addResponseToContext($context, $response, $options);
9898

99-
$this->logger->log($options[self::class][Options::LEVEL], $message, $this->context[$transactionId]);
100-
unset($this->context[$transactionId]);
99+
$this->logger->log($options[self::class][Options::LEVEL], $message, $context);
101100

102101
return resolve($response);
103102
}
@@ -114,8 +113,14 @@ public function error(
114113
string $transactionId,
115114
array $options = []
116115
): CancellablePromiseInterface {
116+
if (!isset($this->context[$transactionId])) {
117+
return reject($throwable);
118+
}
119+
120+
$context = $this->context[$transactionId];
121+
unset($this->context[$transactionId]);
122+
117123
if (!isset($options[self::class][Options::ERROR_LEVEL])) {
118-
unset($this->context[$transactionId]);
119124
return reject($throwable);
120125
}
121126

@@ -126,20 +131,19 @@ public function error(
126131
$response = $throwable->getResponse();
127132
}
128133
if ($response instanceof ResponseInterface) {
129-
$this->addResponseToContext($response, $transactionId, $options);
134+
$context = $this->addResponseToContext($context, $response, $options);
130135
}
131136

132-
$this->context[$transactionId][self::ERROR]['code'] = $throwable->getCode();
133-
$this->context[$transactionId][self::ERROR]['file'] = $throwable->getFile();
134-
$this->context[$transactionId][self::ERROR]['line'] = $throwable->getLine();
135-
$this->context[$transactionId][self::ERROR]['trace'] = $throwable->getTraceAsString();
137+
$context[self::ERROR]['code'] = $throwable->getCode();
138+
$context[self::ERROR]['file'] = $throwable->getFile();
139+
$context[self::ERROR]['line'] = $throwable->getLine();
140+
$context[self::ERROR]['trace'] = $throwable->getTraceAsString();
136141

137142
if (method_exists($throwable, 'getContext')) {
138-
$this->context[$transactionId][self::ERROR]['context'] = $throwable->getContext();
143+
$context[self::ERROR]['context'] = $throwable->getContext();
139144
}
140145

141-
$this->logger->log($options[self::class][Options::ERROR_LEVEL], $message, $this->context[$transactionId]);
142-
unset($this->context[$transactionId]);
146+
$this->logger->log($options[self::class][Options::ERROR_LEVEL], $message, $context);
143147

144148
return reject($throwable);
145149
}
@@ -149,23 +153,39 @@ public function error(
149153
* @param array $headers
150154
* @param array $ignoreHeaders
151155
*/
152-
protected function iterateHeaders(string $prefix, string $transactionId, array $headers, array $ignoreHeaders)
153-
{
156+
protected function iterateHeaders(
157+
array $context,
158+
string $prefix,
159+
array $headers,
160+
array $ignoreHeaders
161+
): array {
154162
foreach ($headers as $header => $value) {
155163
if (in_array($header, $ignoreHeaders)) {
156164
continue;
157165
}
158166

159-
$this->context[$transactionId][$prefix]['headers'][$header] = $value;
167+
$context[$prefix]['headers'][$header] = $value;
160168
}
169+
170+
return $context;
161171
}
162172

163-
private function addResponseToContext(ResponseInterface $response, string $transactionId, array $options)
164-
{
165-
$this->context[$transactionId][self::RESPONSE]['status_code'] = $response->getStatusCode();
166-
$this->context[$transactionId][self::RESPONSE]['status_reason'] = $response->getReasonPhrase();
167-
$this->context[$transactionId][self::RESPONSE]['protocol_version'] = $response->getProtocolVersion();
173+
private function addResponseToContext(
174+
array $context,
175+
ResponseInterface $response,
176+
array $options
177+
): array {
178+
$context[self::RESPONSE]['status_code'] = $response->getStatusCode();
179+
$context[self::RESPONSE]['status_reason'] = $response->getReasonPhrase();
180+
$context[self::RESPONSE]['protocol_version'] = $response->getProtocolVersion();
168181
$ignoreHeaders = $options[self::class][Options::IGNORE_HEADERS] ?? [];
169-
$this->iterateHeaders(self::RESPONSE, $transactionId, $response->getHeaders(), $ignoreHeaders);
182+
$context = $this->iterateHeaders(
183+
$context,
184+
self::RESPONSE,
185+
$response->getHeaders(),
186+
$ignoreHeaders
187+
);
188+
189+
return $context;
170190
}
171191
}

0 commit comments

Comments
 (0)