Skip to content

Commit f0826f8

Browse files
authored
Swap plain text format logs to json with skip of connection logs
Swap plain text format logs to json with skip of connection logs
2 parents 443ba9c + a793018 commit f0826f8

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

exporter/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ RUN composer install --no-dev --prefer-dist --no-interaction --optimize-autoload
2020

2121
COPY ./ ./
2222

23-
CMD ["php", "-S", "0.0.0.0:8000", "-t", "public"]
23+
CMD ["/bin/ash", "-o", "pipefail", "-c", "php -S 0.0.0.0:8000 -t public 2>&1 | php /exporter/log-formatter.php"]
2424

exporter/log-formatter.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
ob_implicit_flush(true);
6+
7+
while (($line = fgets(STDIN)) !== false) {
8+
$line = rtrim($line, "\r\n");
9+
if ($line === '') {
10+
continue;
11+
}
12+
13+
// [Day Mon DD HH:MM:SS YYYY] ip:port [status]: METHOD /path
14+
if (preg_match('/^\[([^\]]+)\] (\S+?):\d+ \[(\d+)\]: (\w+) (\S+)/', $line, $m)) {
15+
echo json_encode([
16+
'time_local' => $m[1],
17+
'remote_addr' => $m[2],
18+
'status' => $m[3],
19+
'request' => $m[4] . ' ' . $m[5],
20+
], JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE) . "\n";
21+
continue;
22+
}
23+
24+
// Skip connection-level noise: Accepted / Closing
25+
if (preg_match('/\b(?:Accepted|Closing)\b/', $line)) {
26+
continue;
27+
}
28+
29+
// Fallback for unexpected lines (e.g. PHP errors)
30+
echo json_encode([
31+
'time_local' => date('D M j H:i:s Y'),
32+
'message' => $line,
33+
], JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE) . "\n";
34+
}

0 commit comments

Comments
 (0)