Skip to content

Commit d62e013

Browse files
committed
FEATURE: Format stdout with eel helper
If a line contains json output it is rendered as toggable details element
1 parent 71a4ac5 commit d62e013

3 files changed

Lines changed: 65 additions & 6 deletions

File tree

Classes/Eel/ModuleHelper.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flowpack\DecoupledContentStore\Eel;
6+
7+
use Neos\Eel\ProtectedContextAwareInterface;
8+
use Neos\Flow\Annotations as Flow;
9+
10+
/**
11+
* @Flow\Scope("singleton")
12+
*/
13+
class ModuleHelper implements ProtectedContextAwareInterface
14+
{
15+
public function formatStdOutput(string $stdOut): string
16+
{
17+
$escapedString = $stdOut;
18+
$lines = array_reverse(array_filter(preg_split("/\r\n|\n|\r/", $escapedString)));
19+
$lineCount = count($lines);
20+
21+
$formattedLines = array_map(static function (string $line, int $index) use ($lineCount) {
22+
// Extract additional JSON data
23+
preg_match("/\{.*}/", $line, $jsonMatches);
24+
$jsonData = array_filter(array_map(static function (string $match) {
25+
$matchData = json_decode($match, true);
26+
return $matchData['message'] ?? $matchData;
27+
}, $jsonMatches));
28+
if (count($jsonData) === 1) {
29+
$jsonData = array_shift($jsonData);
30+
}
31+
32+
// Remove additional JSON data from log line
33+
$line = preg_replace("/\{.*}/", '', $line);
34+
35+
// Add highlighting for log levels
36+
$line = preg_replace("/(DEBUG|WARNING|ERROR|INFO): (.*)/", "<span class=\"log-level-$1\">$1:</span> <span class=\"log-content-$1\">$2</span>", htmlSpecialChars($line));
37+
38+
// Add line numbers
39+
$line = ($lineCount - $index) . ': ' . $line;
40+
41+
// Insert formatted JSON data
42+
if ($jsonData) {
43+
$isDebug = strpos($line, 'DEBUG:') !== false;
44+
$jsonString = "\n<span class=\"json\">" . json_encode($jsonData, JSON_PRETTY_PRINT) . "</span>";
45+
$line = $isDebug ? '<details class="json-details">' . '<summary>' . $line . '</summary>' . $jsonString . '</details>' : $line . $jsonString;
46+
}
47+
48+
// Wrap in <pre> tags
49+
return '<pre>' . $line . '</pre>';
50+
}, $lines, range(1, $lineCount));
51+
52+
return implode("\n", $formattedLines);
53+
}
54+
55+
public function allowsCallOfMethod($methodName): bool
56+
{
57+
return true;
58+
}
59+
}

Configuration/Settings.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,7 @@ Neos:
180180
routes:
181181
'Flowpack.DecoupledContentStore':
182182
position: 'before Neos.Neos'
183+
184+
Fusion:
185+
defaultContext:
186+
Flowpack.DecoupledContentStore: Flowpack\DecoupledContentStore\Eel\ModuleHelper

Resources/Private/BackendFusion/Integration/Backend.Details.fusion

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Flowpack.DecoupledContentStore.BackendController.details = Neos.Fusion:Component
88
// - redisContentStores: array of all configured content store identifiers
99
// - isPrimary: bool
1010

11-
stdOutLines = ${Array.filter(String.pregSplit(String.htmlSpecialChars(jobLogs.stdout), "/\r\n|\n|\r/"))}
11+
stdOutLines = ${Flowpack.DecoupledContentStore.formatStdOutput(jobLogs.stdout)}
1212

1313
renderer = afx`
1414
<div id="app">
@@ -48,11 +48,7 @@ Flowpack.DecoupledContentStore.BackendController.details = Neos.Fusion:Component
4848
</div>
4949

5050
<h2 class="text-3xl py-5">Log output for {detailTaskName}</h2>
51-
<div title="stdout" class="stdout grid gap-2">
52-
<Neos.Fusion:Loop items={Array.reverse(props.stdOutLines)} iterationName="iteration">
53-
<pre>{Array.length(props.stdOutLines) - iteration.index}: {String.pregReplace(item, "/(DEBUG|WARNING|ERROR|INFO): (.*)/", "<span class=\"log-level-$1\">$1:</span> <span class=\"log-content-$1\">$2</span>")}</pre>
54-
</Neos.Fusion:Loop>
55-
</div>
51+
<div title="stdout" class="stdout grid gap-2">{props.stdOutLines}</div>
5652
</Neos.Fusion:Fragment>
5753
<p @if.notData={!detailsData}>
5854
No data exists for this release in Redis.

0 commit comments

Comments
 (0)