Skip to content

Commit

Permalink
Merge pull request #261 from opcodesio/bug/horizon-log-fixes
Browse files Browse the repository at this point in the history
fix Horizon log processing edge cases
  • Loading branch information
arukompas authored Aug 17, 2023
2 parents a7c77b1 + a87cd3e commit 85f7389
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion public/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/app.js": "/app.js?id=e7bdf4c9df5545d0907880d43b7adf71",
"/app.js": "/app.js?id=c8c3c417b14e0e0f6f9d15caea9a1d4a",
"/app.css": "/app.css?id=2644d93568c08a41a0612c7c2c38618e",
"/img/log-viewer-128.png": "/img/log-viewer-128.png?id=d576c6d2e16074d3f064e60fe4f35166",
"/img/log-viewer-32.png": "/img/log-viewer-32.png?id=f8ec67d10f996aa8baf00df3b61eea6d",
Expand Down
3 changes: 2 additions & 1 deletion resources/js/components/BaseLogTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ const clearQuery = () => {
}
const getDataAtPath = (obj, path) => {
return String(path.split('.').reduce((acc, part) => acc && acc[part], obj));
const value = path.split('.').reduce((acc, part) => acc && acc[part], obj);
return typeof value === 'undefined' ? '' : String(value);
}
const hasContext = (log) => {
Expand Down
2 changes: 1 addition & 1 deletion src/Logs/HorizonLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class HorizonLog extends Log
{
public static string $name = 'Laravel Horizon';
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+)/';
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';
public static string $levelClass = HorizonStatusLevel::class;
public static array $columns = [
['label' => 'Datetime', 'data_path' => 'datetime'],
Expand Down
42 changes: 41 additions & 1 deletion tests/Unit/HorizonLogs/HorizonNewLogsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Opcodes\LogViewer\Logs\LogType;

it('can process new Horizon logs', function () {
$file = generateLogFile('horizon_new_dummy.log', content: <<<EOF
$file = generateLogFile('horizon_new.log', content: <<<EOF
Horizon started successfully.
2023-07-22 16:13:33 App\Jobs\TestJob ............................... RUNNING
2023-07-22 16:13:34 App\Jobs\TestJob ............................... 1s DONE
Expand All @@ -29,3 +29,43 @@
->and($logs[1]->message)->toBe('App\Jobs\TestJob')
->and($logs[1]->context['duration'])->toBe('1s');
});

it('processes weird cases', function () {
$file = generateLogFile('horizon_new_weird.log', content: <<<EOF
Horizon started successfully.
2023-08-17 07:22:32 App\Jobs\TestJob 1 s DONE
2023-08-18 06:05:03 App\Jobs\TestJob 32 s. DONE
2023-08-19 04:31:07 App\Jobs\TestJob .... 12 sek DONE
2023-08-20 02:15:58 App\Jobs\TestJob 2023-08-20 02:15:59 App\Jobs\TestJob 25.14ms DONE
EOF);
$file = new LogFile($file->path);

expect($file->type()->value)->toBe(LogType::HORIZON); // HorizonLog

$logReader = $file->logs()->scan();

expect($logs = $logReader->get())->toHaveCount(4)
->and($logs[0]->datetime->toDateTimeString())->toBe('2023-08-17 07:22:32')
->and($logs[0]->level)->toBe('DONE')
->and($logs[0]->getLevel())->toBeInstanceOf(HorizonStatusLevel::class)
->and($logs[0]->getLevel()->getName())->toBe('Done')
->and($logs[0]->message)->toBe('App\Jobs\TestJob')
->and($logs[0]->context['duration'])->toBe('1 s')

->and($logs[1]->datetime->toDateTimeString())->toBe('2023-08-18 06:05:03')
->and($logs[1]->level)->toBe('DONE')
->and($logs[1]->message)->toBe('App\Jobs\TestJob')
->and($logs[1]->context['duration'])->toBe('32 s.')

->and($logs[2]->datetime->toDateTimeString())->toBe('2023-08-19 04:31:07')
->and($logs[2]->level)->toBe('DONE')
->and($logs[2]->message)->toBe('App\Jobs\TestJob')
->and($logs[2]->context['duration'])->toBe('12 sek')

// this one is weird, but let's drop the incomplete beginning and assume the rest of the line is
// the correct log entry.
->and($logs[3]->datetime->toDateTimeString())->toBe('2023-08-20 02:15:59')
->and($logs[3]->level)->toBe('DONE')
->and($logs[3]->message)->toBe('App\Jobs\TestJob')
->and($logs[3]->context['duration'])->toBe('25.14ms');
});

0 comments on commit 85f7389

Please sign in to comment.