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

support for unicode character case-sensitivity when searching #342

Merged
merged 1 commit into from
Mar 1, 2024
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 src/Concerns/LogReader/CanFilterUsingIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected function setQuery(?string $query = null): static
$this->only(null);
$this->onlyShowIndex = intval(explode(':', $query)[1]);
} elseif (! empty($query)) {
$query = '~'.$query.'~i';
$query = '~'.$query.'~iu';

Utils::validateRegex($query);

Expand Down
2 changes: 2 additions & 0 deletions src/Readers/IndexedLogReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ public function scan(?int $maxBytesToScan = null, bool $force = false): static
$lvl = null;

try {
// first, let's see if it matches the new log entry. Does not take search query into account yet.
$lineMatches = $this->logClass::matches(trim($line), $ts, $lvl);
} catch (SkipLineException $exception) {
continue;
}

if ($lineMatches) {
if ($currentLog !== '') {
// Now, let's see if it matches the search query if set.
if (is_null($this->query) || preg_match($this->query, $currentLog)) {
$logIndex->addToIndex($currentLogPosition, $currentTimestamp ?? 0, $currentLogLevel, $currentIndex);
}
Expand Down
47 changes: 47 additions & 0 deletions tests/Feature/LogsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,50 @@

expect($response->json('logs'))->toHaveCount(count($logEntries));
});

test('simple characters can be searched case-insensitive', function () {
$logEntries = [
makeLaravelLogEntry(message: 'error'),
makeLaravelLogEntry(message: 'Error'),
makeLaravelLogEntry(message: 'eRrOr'),
makeLaravelLogEntry(message: 'ERROR'),
makeLaravelLogEntry(message: 'simple text'),
];
$file = generateLogFile('logsearchtest.log', implode(PHP_EOL, $logEntries));

// first, just to be sure that we're getting all the logs without any query
$response = getJson(route('log-viewer.logs', ['file' => $file->identifier]));
expect($response->json('logs'))->toHaveCount(count($logEntries));

// now, with the query. Re-instantiate the log reader to make sure we don't have anything cached.
\Opcodes\LogViewer\Readers\IndexedLogReader::clearInstance($file);
$response = getJson(route('log-viewer.logs', [
'file' => $file->identifier,
'query' => 'error',
]));
expect($response->json('logs'))->toHaveCount(4);

});

test('unicode characters can be searched case-insensitive', function () {
$logEntries = [
makeLaravelLogEntry(message: 'ошибка'),
makeLaravelLogEntry(message: 'Ошибка'),
makeLaravelLogEntry(message: 'ошибкА'),
makeLaravelLogEntry(message: 'ОШИБКА'),
makeLaravelLogEntry(message: 'simple text'),
];
$file = generateLogFile('logunicodetest.log', implode(PHP_EOL, $logEntries));

// first, just to be sure that we're getting all the logs without any query
$response = getJson(route('log-viewer.logs', ['file' => $file->identifier]));
expect($response->json('logs'))->toHaveCount(count($logEntries));

// now, with the query. Re-instantiate the log reader to make sure we don't have anything cached.
\Opcodes\LogViewer\Readers\IndexedLogReader::clearInstance($file);
$response = getJson(route('log-viewer.logs', [
'file' => $file->identifier,
'query' => 'ошибка',
]));
expect($response->json('logs'))->toHaveCount(4);
});
Loading