Skip to content

Commit 27b04d7

Browse files
authored
Merge pull request #266 from opcodesio/bug/2.x-incorrect-laravel-context-extraction
v2 - fix context extraction for Laravel logs
2 parents 583a2fa + 1368722 commit 27b04d7

File tree

8 files changed

+32
-12
lines changed

8 files changed

+32
-12
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "opcodesio/log-viewer",
3-
"version": "v2.5.4",
3+
"version": "v2.5.5",
44
"description": "Fast and easy-to-use log viewer for your Laravel application",
55
"keywords": [
66
"arukompas",

src/Log.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ public function __construct(
4141
$this->index = $index;
4242
$this->fileIdentifier = $fileIdentifier;
4343
$this->filePosition = $filePosition;
44-
$text = mb_convert_encoding(rtrim($text, "\t\n\r"), 'UTF-8', 'UTF-8');
44+
$this->fullText = mb_convert_encoding(rtrim($text, "\t\n\r"), 'UTF-8', 'UTF-8');
4545
$this->fullTextLength = strlen($text);
4646

47+
$this->extractContextsFromFullText();
48+
4749
$matches = [];
48-
[$firstLine, $theRestOfIt] = explode("\n", Str::finish($text, "\n"), 2);
50+
[$firstLine, $theRestOfIt] = explode("\n", Str::finish($this->fullText, "\n"), 2);
4951

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

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

7981
if (session()->get('log-viewer:shorter-stack-traces', false)) {
8082
$excludes = config('log-viewer.shorter_stack_trace_excludes', []);
8183
$emptyLineCharacter = ' ...';
82-
$lines = explode("\n", $text);
84+
$lines = explode("\n", $this->fullText);
8385
$filteredLines = [];
8486
foreach ($lines as $line) {
8587
$shouldExclude = false;
@@ -96,17 +98,15 @@ public function __construct(
9698
$filteredLines[] = $line;
9799
}
98100
}
99-
$text = implode("\n", $filteredLines);
101+
$this->fullText = implode("\n", $filteredLines);
100102
}
101103

102-
if (strlen($text) > LogViewer::maxLogSize()) {
103-
$text = Str::limit($text, LogViewer::maxLogSize());
104+
if (strlen($this->fullText) > LogViewer::maxLogSize()) {
105+
$this->fullText = Str::limit($this->fullText, LogViewer::maxLogSize());
104106
$this->fullTextIncomplete = true;
105107
}
106108

107-
$this->fullText = trim($text);
108-
109-
$this->extractContextsFromFullText();
109+
$this->fullText = trim($this->fullText);
110110
}
111111

112112
public function fullTextMatches(string $query = null): bool

tests/Feature/Authorization/CanDownloadFoldersTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Illuminate\Support\Facades\Gate;
44
use Opcodes\LogViewer\Facades\LogViewer;
55
use Opcodes\LogViewer\LogFolder;
6+
67
use function Pest\Laravel\get;
78

89
test('can download every folder by default', function () {

tests/Feature/Authorization/CanDownloadLogFileTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Illuminate\Support\Facades\Gate;
44
use Opcodes\LogViewer\LogFile;
5+
56
use function Pest\Laravel\get;
67

78
test('can download every file by default', function () {

tests/Feature/Authorization/CanViewLogViewerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Illuminate\Support\Facades\Gate;
44
use Opcodes\LogViewer\Facades\LogViewer;
5+
56
use function Pest\Laravel\get;
67

78
test('can define an "auth" callback for authorization', function () {

tests/Feature/ClearingFileCacheTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Opcodes\LogViewer\Facades\LogViewer;
55
use Opcodes\LogViewer\LogIndex;
66
use Opcodes\LogViewer\Utils\GenerateCacheKey;
7+
78
use function PHPUnit\Framework\assertNotSame;
89

910
beforeEach(function () {

tests/Feature/LogViewerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Opcodes\LogViewer\Facades\LogViewer;
4+
45
use function PHPUnit\Framework\assertContains;
56
use function PHPUnit\Framework\assertNotContains;
67

tests/Unit/LogTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Opcodes\LogViewer\Level;
44
use Opcodes\LogViewer\Log;
5+
56
use function PHPUnit\Framework\assertEquals;
67

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

8788
assertEquals('arunas', $log->contexts[0]['permalink'] ?? null);
8889
assertEquals('Initiating facebook login.', $log->text);
89-
assertEquals(str_replace($jsonString, '', $logText), $log->fullText);
90+
assertEquals(trim(str_replace($jsonString, '', $logText)), $log->fullText);
9091
assertEquals(json_decode($jsonString, true), $log->contexts[0]);
9192
});
9293

@@ -171,3 +172,17 @@
171172
$expectedTime = \Carbon\Carbon::parse('2022-11-07 17:51:33', 'UTC')->tz($tz)->toDateTimeString();
172173
assertEquals($expectedTime, $log->time->toDateTimeString());
173174
});
175+
176+
it('strips extracted context when there\'s multiple contexts available', function () {
177+
config(['log-viewer.strip_extracted_context' => true]);
178+
$logText = <<<'EOF'
179+
[2023-08-16 14:00:25] testing.INFO: Test message. ["one","two"] {"memory_usage":"78 MB","process_id":1234}
180+
EOF;
181+
182+
$log = new Log(0, $logText, 'laravel.log', 0);
183+
184+
assertEquals('Test message.', $log->text);
185+
assertEquals(2, count($log->contexts));
186+
assertEquals(['one', 'two'], $log->contexts[0]);
187+
assertEquals(['memory_usage' => '78 MB', 'process_id' => 1234], $log->contexts[1]);
188+
});

0 commit comments

Comments
 (0)