Skip to content

Commit

Permalink
Merge pull request #266 from opcodesio/bug/2.x-incorrect-laravel-cont…
Browse files Browse the repository at this point in the history
…ext-extraction

v2 - fix context extraction for Laravel logs
  • Loading branch information
arukompas authored Aug 21, 2023
2 parents 583a2fa + 1368722 commit 27b04d7
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opcodesio/log-viewer",
"version": "v2.5.4",
"version": "v2.5.5",
"description": "Fast and easy-to-use log viewer for your Laravel application",
"keywords": [
"arukompas",
Expand Down
20 changes: 10 additions & 10 deletions src/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ public function __construct(
$this->index = $index;
$this->fileIdentifier = $fileIdentifier;
$this->filePosition = $filePosition;
$text = mb_convert_encoding(rtrim($text, "\t\n\r"), 'UTF-8', 'UTF-8');
$this->fullText = mb_convert_encoding(rtrim($text, "\t\n\r"), 'UTF-8', 'UTF-8');
$this->fullTextLength = strlen($text);

$this->extractContextsFromFullText();

$matches = [];
[$firstLine, $theRestOfIt] = explode("\n", Str::finish($text, "\n"), 2);
[$firstLine, $theRestOfIt] = explode("\n", Str::finish($this->fullText, "\n"), 2);

// sometimes, even the first line will have a HUGE exception with tons of debug data all in one line,
// so in order to properly match, we must have a smaller first line...
Expand Down Expand Up @@ -74,12 +76,12 @@ public function __construct(
}

$this->text = trim($firstLineText);
$text = $firstLineText.($matches[8] ?? '').implode('', $firstLineSplit)."\n".$theRestOfIt;
$this->fullText = $firstLineText.($matches[8] ?? '').implode('', $firstLineSplit)."\n".$theRestOfIt;

if (session()->get('log-viewer:shorter-stack-traces', false)) {
$excludes = config('log-viewer.shorter_stack_trace_excludes', []);
$emptyLineCharacter = ' ...';
$lines = explode("\n", $text);
$lines = explode("\n", $this->fullText);
$filteredLines = [];
foreach ($lines as $line) {
$shouldExclude = false;
Expand All @@ -96,17 +98,15 @@ public function __construct(
$filteredLines[] = $line;
}
}
$text = implode("\n", $filteredLines);
$this->fullText = implode("\n", $filteredLines);
}

if (strlen($text) > LogViewer::maxLogSize()) {
$text = Str::limit($text, LogViewer::maxLogSize());
if (strlen($this->fullText) > LogViewer::maxLogSize()) {
$this->fullText = Str::limit($this->fullText, LogViewer::maxLogSize());
$this->fullTextIncomplete = true;
}

$this->fullText = trim($text);

$this->extractContextsFromFullText();
$this->fullText = trim($this->fullText);
}

public function fullTextMatches(string $query = null): bool
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
17 changes: 16 additions & 1 deletion tests/Unit/LogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Opcodes\LogViewer\Level;
use Opcodes\LogViewer\Log;

use function PHPUnit\Framework\assertEquals;

it('can understand the default Laravel log format', function () {
Expand Down Expand Up @@ -86,7 +87,7 @@

assertEquals('arunas', $log->contexts[0]['permalink'] ?? null);
assertEquals('Initiating facebook login.', $log->text);
assertEquals(str_replace($jsonString, '', $logText), $log->fullText);
assertEquals(trim(str_replace($jsonString, '', $logText)), $log->fullText);
assertEquals(json_decode($jsonString, true), $log->contexts[0]);
});

Expand Down Expand Up @@ -171,3 +172,17 @@
$expectedTime = \Carbon\Carbon::parse('2022-11-07 17:51:33', 'UTC')->tz($tz)->toDateTimeString();
assertEquals($expectedTime, $log->time->toDateTimeString());
});

it('strips extracted context when there\'s multiple contexts available', function () {
config(['log-viewer.strip_extracted_context' => true]);
$logText = <<<'EOF'
[2023-08-16 14:00:25] testing.INFO: Test message. ["one","two"] {"memory_usage":"78 MB","process_id":1234}
EOF;

$log = new Log(0, $logText, 'laravel.log', 0);

assertEquals('Test message.', $log->text);
assertEquals(2, count($log->contexts));
assertEquals(['one', 'two'], $log->contexts[0]);
assertEquals(['memory_usage' => '78 MB', 'process_id' => 1234], $log->contexts[1]);
});

0 comments on commit 27b04d7

Please sign in to comment.