Skip to content

Commit

Permalink
Merge pull request #265 from opcodesio/bug/incorrectly-extracted-lara…
Browse files Browse the repository at this point in the history
…vel-context

fix an edge case in Laravel context extraction
  • Loading branch information
arukompas authored Aug 21, 2023
2 parents d83a5be + 4eec983 commit 9ed2263
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Logs/LaravelLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ protected function parseText(array &$matches = []): void
$this->text = mb_convert_encoding(rtrim($this->text, "\t\n\r"), 'UTF-8', 'UTF-8');
$length = strlen($this->text);

$this->extractContextsFromFullText();

$this->extra['log_size'] = $length;
$this->extra['log_size_formatted'] = Utils::bytesForHumans($length);

Expand Down Expand Up @@ -89,8 +91,6 @@ protected function parseText(array &$matches = []): void
}

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

$this->extractContextsFromFullText();
}

protected function fillMatches(array $matches = []): void
Expand Down
14 changes: 14 additions & 0 deletions tests/Unit/LogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,17 @@
$expectedTime = \Carbon\Carbon::parse('2022-11-07 17:51:33', 'UTC')->tz($tz)->toDateTimeString();
assertEquals($expectedTime, $log->datetime->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 LaravelLog($logText);

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

0 comments on commit 9ed2263

Please sign in to comment.