Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Horizon log processing edge cases #261

Merged
merged 7 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions tests/Feature/Authorization/CanDownloadFoldersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Illuminate\Support\Facades\Gate;
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\LogFolder;

use function Pest\Laravel\get;

test('can download every folder by default', function () {
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Authorization/CanDownloadLogFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Support\Facades\Gate;
use Opcodes\LogViewer\LogFile;

use function Pest\Laravel\get;

test('can download every file by default', function () {
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Authorization/CanViewLogViewerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Support\Facades\Gate;
use Opcodes\LogViewer\Facades\LogViewer;

use function Pest\Laravel\get;

test('can define an "auth" callback for authorization', function () {
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/ClearingFileCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\LogIndex;
use Opcodes\LogViewer\Utils\GenerateCacheKey;

use function PHPUnit\Framework\assertNotSame;

beforeEach(function () {
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/LogViewerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Opcodes\LogViewer\Facades\LogViewer;

use function PHPUnit\Framework\assertContains;
use function PHPUnit\Framework\assertNotContains;

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');
});
1 change: 1 addition & 0 deletions tests/Unit/LogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Opcodes\LogViewer\LogLevels\LaravelLogLevel;
use Opcodes\LogViewer\Logs\LaravelLog;

use function PHPUnit\Framework\assertEquals;

it('can understand the default Laravel log format', function () {
Expand Down