-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogFormatter.php
More file actions
371 lines (307 loc) · 11.3 KB
/
Copy pathLogFormatter.php
File metadata and controls
371 lines (307 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
/**
* @author Aaron Francis <aaron@tryhardstudios.com>
*
* @link https://aaronfrancis.com
* @link https://x.com/aarondfrancis
*/
namespace SoloTerm\Vtail\Formatting;
/**
* Parses and formats Laravel log lines with stack trace handling.
*
* Maintains state while processing lines to:
* - Track consecutive vendor frames for collapsing
* - Detect stack trace boundaries for bordered formatting
* - Handle pre-wrapped continuation lines within traces
*
* State is reset via reset() when processing a new log file or after truncation.
*/
class LogFormatter
{
protected int $contentWidth;
protected bool $wrapLines = true;
/**
* Current vendor group ID for tracking consecutive vendor frames.
*/
protected int $vendorGroupId = 0;
/**
* Whether we're currently in a vendor group.
*/
protected bool $inVendorGroup = false;
/**
* Whether we're currently inside a stack trace.
*/
protected bool $inStackTrace = false;
public function __construct(int $contentWidth)
{
$this->contentWidth = $contentWidth;
}
/**
* Enable or disable line wrapping.
*/
public function setWrapLines(bool $wrapLines): void
{
$this->wrapLines = $wrapLines;
}
/**
* Set the content width for formatting.
*/
public function setContentWidth(int $width): void
{
$this->contentWidth = $width;
}
/**
* Reset formatter state for a fresh log file.
*/
public function reset(): void
{
$this->vendorGroupId = 0;
$this->inVendorGroup = false;
$this->inStackTrace = false;
}
/**
* Format all lines into a LineCollection.
*
* @param array<string> $rawLines
*/
public function formatLines(array $rawLines): LineCollection
{
$collection = new LineCollection;
foreach ($rawLines as $index => $rawLine) {
$line = $this->formatLine($rawLine, $index);
if ($line !== null) {
$collection->addLine($line);
}
}
return $collection;
}
/**
* Format only new lines starting from a given index.
* Does not reset state, allowing incremental processing.
*
* @param array<string> $rawLines
* @return array<Line>
*/
public function formatNewLines(array $rawLines, int $startIndex): array
{
$newLines = [];
for ($i = $startIndex; $i < count($rawLines); $i++) {
$line = $this->formatLine($rawLines[$i], $i);
if ($line !== null) {
$newLines[] = $line;
}
}
return $newLines;
}
/**
* Format a single log line into a Line object.
*/
public function formatLine(string $rawLine, int $index): ?Line
{
// Content line structure: " │ " + content + " │" = 5 chars for borders
$traceContentWidth = $this->contentWidth - 5;
// Footer: closing JSON exception object
if (str_contains($rawLine, '"}') && trim(AnsiAware::plain($rawLine)) === '"}') {
$this->endVendorGroup();
$this->inStackTrace = false;
return new Line(
content: $rawLine,
formattedLines: [AnsiAware::dim(' ╰'.str_repeat('═', $this->contentWidth - 3).'╯')],
originalIndex: $index,
);
}
// Exception header
if (str_contains($rawLine, '{"exception":"[object] ')) {
$this->endVendorGroup();
[$formattedLines, $fullWrapCount] = $this->formatExceptionHeader($rawLine);
return new Line(
content: $rawLine,
formattedLines: $formattedLines,
originalIndex: $index,
fullWrapCount: $fullWrapCount,
);
}
// Stacktrace header
if (str_contains($rawLine, '[stacktrace]')) {
$this->endVendorGroup();
$this->inStackTrace = true;
return new Line(
content: $rawLine,
formattedLines: [AnsiAware::dim(' ╭─Trace'.str_repeat('─', $this->contentWidth - 9).'╮')],
originalIndex: $index,
isStackFrame: true,
);
}
// Stack trace frame
if (preg_match('/#[0-9]+ /', $rawLine)) {
return $this->createStackFrameLine($rawLine, $index, $traceContentWidth);
}
// Stack trace continuation (inside trace but doesn't start with #)
if ($this->inStackTrace) {
return $this->createStackFrameContinuation($rawLine, $index, $traceContentWidth);
}
// Regular line - wrap or truncate
$this->endVendorGroup();
[$formattedLines, $fullWrapCount] = $this->wrapWithCount($rawLine, $this->contentWidth);
return new Line(
content: $rawLine,
formattedLines: $formattedLines,
originalIndex: $index,
fullWrapCount: $fullWrapCount,
);
}
/**
* Create a Line object for a stack frame.
*/
protected function createStackFrameLine(string $rawLine, int $index, int $traceContentWidth): Line
{
// Pad single-digit frame numbers
$line = preg_replace('/^(\e\[0m)?#(\d)(?!\d)/', '$1#0$2', $rawLine);
$isVendor = $this->isVendorFrame($line);
$vendorGroupId = null;
if ($isVendor) {
if (! $this->inVendorGroup) {
$this->vendorGroupId++;
$this->inVendorGroup = true;
}
$vendorGroupId = $this->vendorGroupId;
} else {
$this->endVendorGroup();
}
// Format the line fully (both vendor and non-vendor)
$line = $this->highlightFileOnly($line);
[$wrappedLines, $fullWrapCount] = $this->wrapWithCount($line, $traceContentWidth, 4);
$formattedLines = array_map(
fn ($l) => AnsiAware::dim(' │ ').AnsiAware::pad($l, $traceContentWidth).AnsiAware::dim(' │'),
$wrappedLines
);
return new Line(
content: $rawLine,
formattedLines: $formattedLines,
originalIndex: $index,
fullWrapCount: $fullWrapCount,
isStackFrame: true,
isVendorFrame: $isVendor,
vendorGroupId: $vendorGroupId,
);
}
/**
* Create a Line object for a stack frame continuation (pre-wrapped in log file).
*/
protected function createStackFrameContinuation(string $rawLine, int $index, int $traceContentWidth): Line
{
// Inherit vendor status from current state (don't break the group)
$vendorGroupId = $this->inVendorGroup ? $this->vendorGroupId : null;
[$wrappedLines, $fullWrapCount] = $this->wrapWithCount($rawLine, $traceContentWidth, 4);
$formattedLines = array_map(
fn ($l) => AnsiAware::dim(' │ ').AnsiAware::pad($l, $traceContentWidth).AnsiAware::dim(' │'),
$wrappedLines
);
return new Line(
content: $rawLine,
formattedLines: $formattedLines,
originalIndex: $index,
fullWrapCount: $fullWrapCount,
isStackFrame: true,
isVendorFrame: $this->inVendorGroup,
vendorGroupId: $vendorGroupId,
);
}
/**
* End the current vendor group.
*/
protected function endVendorGroup(): void
{
$this->inVendorGroup = false;
}
/**
* Format an exception header line.
*
* @return array{0: array<string>, 1: int} [formattedLines, fullWrapCount]
*/
protected function formatExceptionHeader(string $line): array
{
$parts = explode('{"exception":"[object] ', $line);
[$messageLines, $messageWrapCount] = $this->wrapWithCount($parts[0], $this->contentWidth);
[$exceptionLines, $exceptionWrapCount] = $this->wrapWithCount($parts[1] ?? '', $this->contentWidth - 1);
// Indent exception lines
$exception = array_map(fn ($l) => ' '.$l, $exceptionLines);
$formattedLines = array_merge($messageLines, $exception);
$fullWrapCount = $messageWrapCount + $exceptionWrapCount;
return [$formattedLines, $fullWrapCount];
}
/**
* Highlight the file path in a stack frame, dimming frame number and method.
*/
protected function highlightFileOnly(string $line): string
{
// Pattern handles lines with or without \e[0m prefix
$pattern = '/^(\e\[0m)?(#\d+)(.*?)(:.*)?$/';
$replacement = "\033[2m$2\033[0m$3\033[2m$4\033[0m";
return preg_replace($pattern, $replacement, $line);
}
/**
* Check if a line represents a vendor frame.
*
* A frame is considered vendor if:
* - Path contains /vendor/ (except BoundMethod.php calling App\ code)
* - Frame is {main} (execution root)
*/
public function isVendorFrame(string $line): bool
{
$plain = AnsiAware::plain($line);
return (str_contains($plain, '/vendor/') && ! preg_match("/BoundMethod\.php\([0-9]+\): App/", $plain))
|| str_ends_with($plain, '{main}');
}
/**
* Wrap a line and return both the result and full wrap count.
* Used for scroll position preservation when toggling wrap mode.
*
* @return array{0: array<string>, 1: int} [formattedLines, fullWrapCount]
*/
protected function wrapWithCount(string $line, int $width, int $continuationIndent = 0): array
{
if ($width <= 0) {
return [[$line], 1];
}
$wrapped = $this->wrapLine($line, $width, $continuationIndent);
$fullWrapCount = count($wrapped);
if (! $this->wrapLines && $fullWrapCount > 1) {
// Truncate: keep first line, add indicator
$indicator = AnsiAware::dim(' ...');
$indicatorLen = AnsiAware::mb_strlen($indicator);
$truncated = explode("\n", AnsiAware::wordwrap($wrapped[0], $width - $indicatorLen, "\n", true));
return [[$truncated[0].$indicator], $fullWrapCount];
}
return [$wrapped, $fullWrapCount];
}
/**
* Wrap a line to the given width.
*
* @return array<string>
*/
public function wrapLine(string $line, int $width, int $continuationIndent = 0): array
{
$contWidth = $continuationIndent > 0 ? $width - $continuationIndent : $width;
$firstWrap = explode("\n", AnsiAware::wordwrap($line, $width, "\n", true));
$result = [$firstWrap[0]];
if (count($firstWrap) > 1) {
$remainder = implode('', array_slice($firstWrap, 1));
$contLines = explode("\n", AnsiAware::wordwrap($remainder, $contWidth, "\n", true));
$indent = str_repeat(' ', $continuationIndent);
foreach ($contLines as $contLine) {
if (trim($contLine) !== '') {
$result[] = $indent.$contLine;
}
}
}
// If wrapping is disabled, truncate to first line with indicator
if (! $this->wrapLines && count($result) > 1) {
$indicator = AnsiAware::dim(' ...');
$indicatorLen = AnsiAware::mb_strlen($indicator);
$truncated = explode("\n", AnsiAware::wordwrap($result[0], $width - $indicatorLen, "\n", true));
return [$truncated[0].$indicator];
}
return $result;
}
}