Skip to content

Commit 85f7389

Browse files
authored
Merge pull request #261 from opcodesio/bug/horizon-log-fixes
fix Horizon log processing edge cases
2 parents a7c77b1 + a87cd3e commit 85f7389

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

public/app.js

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

public/mix-manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"/app.js": "/app.js?id=e7bdf4c9df5545d0907880d43b7adf71",
2+
"/app.js": "/app.js?id=c8c3c417b14e0e0f6f9d15caea9a1d4a",
33
"/app.css": "/app.css?id=2644d93568c08a41a0612c7c2c38618e",
44
"/img/log-viewer-128.png": "/img/log-viewer-128.png?id=d576c6d2e16074d3f064e60fe4f35166",
55
"/img/log-viewer-32.png": "/img/log-viewer-32.png?id=f8ec67d10f996aa8baf00df3b61eea6d",

resources/js/components/BaseLogTable.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ const clearQuery = () => {
150150
}
151151
152152
const getDataAtPath = (obj, path) => {
153-
return String(path.split('.').reduce((acc, part) => acc && acc[part], obj));
153+
const value = path.split('.').reduce((acc, part) => acc && acc[part], obj);
154+
return typeof value === 'undefined' ? '' : String(value);
154155
}
155156
156157
const hasContext = (log) => {

src/Logs/HorizonLog.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class HorizonLog extends Log
99
{
1010
public static string $name = 'Laravel Horizon';
11-
public static string $regex = '/\s*(?P<datetime>\d{4}-\d\d-\d\d \d\d:\d\d:\d\d) (?<message>\S+) \.* ?(?<duration>\d+\.?\d*\S+)? (?P<level>\S+)/';
11+
public static string $regex = '/^.*(?P<datetime>\d{4}-\d\d-\d\d \d\d:\d\d:\d\d) (?<message>\S+) \.* ?(?<duration>\d[\d\s\.\w]+)? (?P<level>\S+)\R?/m';
1212
public static string $levelClass = HorizonStatusLevel::class;
1313
public static array $columns = [
1414
['label' => 'Datetime', 'data_path' => 'datetime'],

tests/Unit/HorizonLogs/HorizonNewLogsTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Opcodes\LogViewer\Logs\LogType;
66

77
it('can process new Horizon logs', function () {
8-
$file = generateLogFile('horizon_new_dummy.log', content: <<<EOF
8+
$file = generateLogFile('horizon_new.log', content: <<<EOF
99
Horizon started successfully.
1010
2023-07-22 16:13:33 App\Jobs\TestJob ............................... RUNNING
1111
2023-07-22 16:13:34 App\Jobs\TestJob ............................... 1s DONE
@@ -29,3 +29,43 @@
2929
->and($logs[1]->message)->toBe('App\Jobs\TestJob')
3030
->and($logs[1]->context['duration'])->toBe('1s');
3131
});
32+
33+
it('processes weird cases', function () {
34+
$file = generateLogFile('horizon_new_weird.log', content: <<<EOF
35+
Horizon started successfully.
36+
2023-08-17 07:22:32 App\Jobs\TestJob 1 s DONE
37+
2023-08-18 06:05:03 App\Jobs\TestJob 32 s. DONE
38+
2023-08-19 04:31:07 App\Jobs\TestJob .... 12 sek DONE
39+
2023-08-20 02:15:58 App\Jobs\TestJob 2023-08-20 02:15:59 App\Jobs\TestJob 25.14ms DONE
40+
EOF);
41+
$file = new LogFile($file->path);
42+
43+
expect($file->type()->value)->toBe(LogType::HORIZON); // HorizonLog
44+
45+
$logReader = $file->logs()->scan();
46+
47+
expect($logs = $logReader->get())->toHaveCount(4)
48+
->and($logs[0]->datetime->toDateTimeString())->toBe('2023-08-17 07:22:32')
49+
->and($logs[0]->level)->toBe('DONE')
50+
->and($logs[0]->getLevel())->toBeInstanceOf(HorizonStatusLevel::class)
51+
->and($logs[0]->getLevel()->getName())->toBe('Done')
52+
->and($logs[0]->message)->toBe('App\Jobs\TestJob')
53+
->and($logs[0]->context['duration'])->toBe('1 s')
54+
55+
->and($logs[1]->datetime->toDateTimeString())->toBe('2023-08-18 06:05:03')
56+
->and($logs[1]->level)->toBe('DONE')
57+
->and($logs[1]->message)->toBe('App\Jobs\TestJob')
58+
->and($logs[1]->context['duration'])->toBe('32 s.')
59+
60+
->and($logs[2]->datetime->toDateTimeString())->toBe('2023-08-19 04:31:07')
61+
->and($logs[2]->level)->toBe('DONE')
62+
->and($logs[2]->message)->toBe('App\Jobs\TestJob')
63+
->and($logs[2]->context['duration'])->toBe('12 sek')
64+
65+
// this one is weird, but let's drop the incomplete beginning and assume the rest of the line is
66+
// the correct log entry.
67+
->and($logs[3]->datetime->toDateTimeString())->toBe('2023-08-20 02:15:59')
68+
->and($logs[3]->level)->toBe('DONE')
69+
->and($logs[3]->message)->toBe('App\Jobs\TestJob')
70+
->and($logs[3]->context['duration'])->toBe('25.14ms');
71+
});

0 commit comments

Comments
 (0)