Skip to content

Commit ab3b162

Browse files
committed
Configurable log messages
1 parent 6937657 commit ab3b162

File tree

5 files changed

+137
-26
lines changed

5 files changed

+137
-26
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"require": {
1212
"php": "^7.2",
1313
"api-clients/middleware": "^4.0",
14-
"psr/log": "^1.0"
14+
"psr/log": "^1.0",
15+
"wyrihaximus/string-get-in": "^1.0"
1516
},
1617
"require-dev": {
1718
"api-clients/test-utilities": "^5.1.0",

composer.lock

Lines changed: 86 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/LoggerMiddleware.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@
1212
use Throwable;
1313
use function React\Promise\reject;
1414
use function React\Promise\resolve;
15+
use function WyriHaximus\getIn;
1516

1617
class LoggerMiddleware implements MiddlewareInterface
1718
{
18-
private const REQUEST = 'request';
19-
private const RESPONSE = 'response';
20-
private const ERROR = 'error';
19+
private const REQUEST = 'request';
20+
private const RESPONSE = 'response';
21+
private const ERROR = 'error';
22+
23+
private const MESSAGE_URL = '[{{transaction_id}}] Requesting: {{request.uri}}';
24+
private const MESSAGE_SUCCESSFUL = '[{{transaction_id}}] Request completed with {{response.status_code}}';
25+
private const MESSAGE_ERROR = '[{{transaction_id}}] {{error.message}}';
2126

2227
/**
2328
* @var LoggerInterface
@@ -43,6 +48,10 @@ public function pre(
4348
return resolve($request);
4449
}
4550

51+
$this->context[$transactionId] = [
52+
'transaction_id' => $transactionId,
53+
self::REQUEST => [],
54+
];
4655
$this->context[$transactionId][self::REQUEST]['method'] = $request->getMethod();
4756
$this->context[$transactionId][self::REQUEST]['uri'] = (string)$this->stripQueryItems(
4857
$request->getUri(),
@@ -61,7 +70,7 @@ public function pre(
6170
return resolve($request);
6271
}
6372

64-
$message = 'Requesting: ' . $this->context[$transactionId][self::REQUEST]['uri'];
73+
$message = $this->renderTemplate(self::MESSAGE_URL, $this->context[$transactionId]);
6574
$this->logger->log($options[self::class][Options::URL_LEVEL], $message, $this->context[$transactionId]);
6675

6776
return resolve($request);
@@ -88,10 +97,8 @@ public function post(
8897
return resolve($response);
8998
}
9099

91-
$message = 'Request ' . $transactionId . ' completed.';
92-
93100
$context = $this->addResponseToContext($context, $response, $options);
94-
101+
$message = $this->renderTemplate(self::MESSAGE_SUCCESSFUL, $context);
95102
$this->logger->log($options[self::class][Options::LEVEL], $message, $context);
96103

97104
return resolve($response);
@@ -116,8 +123,6 @@ public function error(
116123
return reject($throwable);
117124
}
118125

119-
$message = $throwable->getMessage();
120-
121126
$response = null;
122127
if (method_exists($throwable, 'getResponse')) {
123128
$response = $throwable->getResponse();
@@ -126,6 +131,7 @@ public function error(
126131
$context = $this->addResponseToContext($context, $response, $options);
127132
}
128133

134+
$context[self::ERROR]['message'] = $throwable->getMessage();
129135
$context[self::ERROR]['code'] = $throwable->getCode();
130136
$context[self::ERROR]['file'] = $throwable->getFile();
131137
$context[self::ERROR]['line'] = $throwable->getLine();
@@ -135,6 +141,7 @@ public function error(
135141
$context[self::ERROR]['context'] = $throwable->getContext();
136142
}
137143

144+
$message = $this->renderTemplate(self::MESSAGE_ERROR, $context);
138145
$this->logger->log($options[self::class][Options::ERROR_LEVEL], $message, $context);
139146

140147
return reject($throwable);
@@ -185,4 +192,16 @@ private function stripQueryItems(UriInterface $uri, array $options): UriInterfac
185192

186193
return $uri->withQuery(http_build_query($query));
187194
}
195+
196+
private function renderTemplate(string $template, array $context): string
197+
{
198+
$keyValues = [];
199+
preg_match_all("|\{\{(.*)\}\}|U", $template, $out, PREG_PATTERN_ORDER);
200+
foreach (array_unique(array_values($out[1])) as $placeHolder) {
201+
$keyValues['{{' . $placeHolder . '}}'] = getIn($context, $placeHolder, '');
202+
}
203+
$template = str_replace(array_keys($keyValues), array_values($keyValues), $template);
204+
205+
return $template;
206+
}
188207
}

src/Options.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ final class Options
99
public const LEVEL = 'level';
1010
public const ERROR_LEVEL = 'error_level';
1111
public const URL_LEVEL = 'url_level';
12+
public const MESSAGE_PRE = 'message_pre';
13+
public const MESSAGE_POST = 'message_post';
14+
public const MESSAGE_ERROR = 'message_error';
1215
}

tests/LoggerMiddlewareTest.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,13 @@ public function testLog()
7575
'X-Ignore-Response' => 'nope',
7676
]
7777
);
78-
$exception = new Exception(
79-
'New Exception'
80-
);
8178

8279
$logger = $this->prophesize(LoggerInterface::class);
8380
$logger->log(
8481
LogLevel::DEBUG,
85-
'Requesting: https://example.com/?dont_strip_this_item=1',
82+
'[abc] Requesting: https://example.com/?dont_strip_this_item=1',
8683
[
84+
'transaction_id' => 'abc',
8785
'request' => [
8886
'method' => 'GET',
8987
'uri' => 'https://example.com/?dont_strip_this_item=1',
@@ -97,8 +95,9 @@ public function testLog()
9795
)->shouldBeCalled();
9896
$logger->log(
9997
LogLevel::DEBUG,
100-
'Request abc completed.',
98+
'[abc] Request completed with 200',
10199
[
100+
'transaction_id' => 'abc',
102101
'request' => [
103102
'method' => 'GET',
104103
'uri' => 'https://example.com/?dont_strip_this_item=1',
@@ -160,8 +159,9 @@ public function getResponse()
160159
$logger = $this->prophesize(LoggerInterface::class);
161160
$logger->log(
162161
LogLevel::ERROR,
163-
$exception->getMessage(),
162+
'[abc] ' . $exception->getMessage(),
164163
[
164+
'transaction_id' => 'abc',
165165
'request' => [
166166
'method' => 'GET',
167167
'uri' => 'https://example.com/',
@@ -180,10 +180,11 @@ public function getResponse()
180180
],
181181
],
182182
'error' => [
183-
'code' => $exception->getCode(),
184-
'file' => $exception->getFile(),
185-
'line' => $exception->getLine(),
186-
'trace' => $exception->getTraceAsString(),
183+
'message' => $exception->getMessage(),
184+
'code' => $exception->getCode(),
185+
'file' => $exception->getFile(),
186+
'line' => $exception->getLine(),
187+
'trace' => $exception->getTraceAsString(),
187188
],
188189
]
189190
)->shouldBeCalled();
@@ -218,8 +219,9 @@ public function testLogErrorNoResponse()
218219
$logger = $this->prophesize(LoggerInterface::class);
219220
$logger->log(
220221
LogLevel::ERROR,
221-
$exception->getMessage(),
222+
'[abc] ' . $exception->getMessage(),
222223
[
224+
'transaction_id' => 'abc',
223225
'request' => [
224226
'method' => 'GET',
225227
'uri' => 'https://example.com/',
@@ -230,10 +232,11 @@ public function testLogErrorNoResponse()
230232
],
231233
],
232234
'error' => [
233-
'code' => $exception->getCode(),
234-
'file' => $exception->getFile(),
235-
'line' => $exception->getLine(),
236-
'trace' => $exception->getTraceAsString(),
235+
'message' => $exception->getMessage(),
236+
'code' => $exception->getCode(),
237+
'file' => $exception->getFile(),
238+
'line' => $exception->getLine(),
239+
'trace' => $exception->getTraceAsString(),
237240
],
238241
]
239242
)->shouldBeCalled();

0 commit comments

Comments
 (0)