Skip to content

Commit

Permalink
fix a bug with new logs sometimes overriding index of older logs
Browse files Browse the repository at this point in the history
  • Loading branch information
arukompas committed Nov 18, 2022
1 parent 7df9296 commit 7d0110e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/LogFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public function logs(): LogReader

public function size(): int
{
clearstatcache();

return filesize($this->path);
}

Expand Down
1 change: 1 addition & 0 deletions src/LogReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ public function scan(int $maxBytesToScan = null, bool $force = false): self
if ($currentLog !== '' && preg_match($logMatchPattern, $currentLog) === 1) {
if ((is_null($this->query) || preg_match($this->query, $currentLog))) {
$logIndex->addToIndex($currentLogPosition, $currentTimestamp, $currentLogLevel, $currentIndex);
$currentIndex++;
}
}

Expand Down
53 changes: 42 additions & 11 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Carbon\Carbon;
use Illuminate\Support\Facades\File;
use Opcodes\LogViewer\LogFile;
use Opcodes\LogViewer\LogIndex;
Expand All @@ -18,30 +19,60 @@

/**
* Generate log files with random data
*
* @param array <int, string> $files
* @return void
*/
function generateLogFiles(array $files): void
function generateLogFiles(array $files, string $content = null, bool $randomContent = false): array
{
foreach ($files as $file) {
$file = storage_path('logs/'.$file);
return array_map(
fn ($file) => generateLogFile($file, $content, $randomContent),
$files
);
}

if (File::exists($file)) {
File::delete($file);
}
function generateLogFile(string $fileName = null, string $content = null, bool $randomContent = false): LogFile
{
if (is_null($fileName)) {
$fileName = \Illuminate\Support\Str::random().'.log';
}

File::put($file, str()->random());
$path = storage_path('logs/'.$fileName);

test()->assertFileExists($file);
if (File::exists($path)) {
File::delete($path);
}

File::put($path, $content ?? ($randomContent ? dummyLogData() : ''));

// we perform a regular PHP assertion, so it doesn't count towards the unit test assertion count.
assert(file_exists($path));

return new LogFile($fileName, $path);
}

function dummyLogData(int $lines = null): string
{
if (is_null($lines)) {
$lines = rand(1, 10);
}

return implode("\n", array_map(
fn ($_) => makeLogEntry(),
range(1, $lines)
));
}

function clearGeneratedLogFiles(): void
{
File::cleanDirectory(storage_path('logs'));
}

function makeLogEntry(Carbon $date = null, string $level = 'debug', string $message = 'Testing log entry'): string
{
$dateFormatted = $date instanceof Carbon ? $date->toDateTimeString() : now()->toDateTimeString();
$level = strtoupper($level);

return "[$dateFormatted] local.$level: $message";
}

function createLogIndex($file = null, $query = null, array $predefinedLogs = []): LogIndex
{
if (is_null($file)) {
Expand Down
41 changes: 41 additions & 0 deletions tests/Unit/LogReaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

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

beforeEach(function () {
$this->file = generateLogFile();
File::append($this->file->path, makeLogEntry());
});

afterEach(fn () => clearGeneratedLogFiles());

it('can scan a log file', function () {
$logReader = $this->file->logs();
expect($logReader->requiresScan())->toBeTrue();

$logReader->scan();

expect($logReader->requiresScan())->toBeFalse()
->and($logReader->index()->total())->toBe(1);
});

it('can re-scan the file after a new entry has been added', function () {
$logReader = $this->file->logs();
$logReader->scan();

\Spatie\TestTime\TestTime::addMinute();

File::append($this->file->path, PHP_EOL.makeLogEntry());

// re-instantiate the log reader to make sure we don't have anything cached
$this->file = new LogFile($this->file->name, $this->file->path);
$logReader = $this->file->logs();
expect($logReader->requiresScan())->toBeTrue();

$logReader->scan();

expect($logReader->requiresScan())->toBeFalse()
->and($logReader->index()->total())->toBe(2)
->and($logReader->index()->getFlatIndex())->toHaveCount(2);
});
File renamed without changes.

0 comments on commit 7d0110e

Please sign in to comment.