Skip to content

Commit

Permalink
refactor how Log Viewer timezone is resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
arukompas committed Mar 1, 2024
1 parent dbfefa1 commit 65021bc
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/Facades/LogViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @see \Opcodes\LogViewer\LogViewerService
*
* @method static string version()
* @method static string timezone()
* @method static bool assetsAreCurrent()
* @method static bool supportsHostsFeature()
* @method static void resolveHostsUsing(callable $callback)
Expand Down
12 changes: 12 additions & 0 deletions src/LogViewerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,23 @@ class LogViewerService
public static string $logFileClass = LogFile::class;
public static string $logReaderClass = IndexedLogReader::class;
protected ?Collection $_cachedFiles = null;
protected string $_cachedTimezone;
protected mixed $authCallback;
protected int $maxLogSizeToDisplay = self::DEFAULT_MAX_LOG_SIZE_TO_DISPLAY;
protected mixed $hostsResolver;
protected string $layout = 'log-viewer::index';

public function timezone(): string
{
if (! isset($this->_cachedTimezone)) {
$this->_cachedTimezone = config('log-viewer.timezone')
?? config('app.timezone')
?? 'UTC';
}

return $this->_cachedTimezone;
}

protected function getLaravelLogFilePaths(): array
{
// Because we'll use the base path as a parameter for `glob`, we should escape any
Expand Down
4 changes: 2 additions & 2 deletions src/Logs/HorizonLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Opcodes\LogViewer\Logs;

use Opcodes\LogViewer\Exceptions\SkipLineException;
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\LogLevels\HorizonStatusLevel;

class HorizonLog extends Log
Expand All @@ -20,8 +21,7 @@ class HorizonLog extends Log
protected function fillMatches(array $matches = []): void
{
$datetime = $this->parseDateTime($matches['datetime'] ?? null);
$timezone = config('log-viewer.timezone', config('app.timezone', 'UTC')) ?? 'UTC';
$this->datetime = $datetime?->setTimezone($timezone);
$this->datetime = $datetime?->setTimezone(LogViewer::timezone());

$this->level = $matches['level'];
$this->message = $matches['message'];
Expand Down
4 changes: 2 additions & 2 deletions src/Logs/HorizonOldLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Opcodes\LogViewer\Logs;

use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\LogLevels\HorizonStatusLevel;

class HorizonOldLog extends Log
Expand All @@ -19,8 +20,7 @@ class HorizonOldLog extends Log
protected function fillMatches(array $matches = []): void
{
$datetime = static::parseDateTime($matches['datetime'] ?? null);
$timezone = config('log-viewer.timezone', config('app.timezone', 'UTC')) ?? 'UTC';
$this->datetime = $datetime?->setTimezone($timezone);
$this->datetime = $datetime?->setTimezone(LogViewer::timezone());

$this->level = $matches['level'];
$this->message = $matches['message'];
Expand Down
4 changes: 2 additions & 2 deletions src/Logs/HttpAccessLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\CarbonInterface;
use Illuminate\Support\Carbon;
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\LogLevels\HttpStatusCodeLevel;

class HttpAccessLog extends Log
Expand Down Expand Up @@ -35,8 +36,7 @@ protected function fillMatches(array $matches = []): void
];

$datetime = static::parseDateTime($matches['datetime'] ?? null);
$timezone = config('log-viewer.timezone', config('app.timezone', 'UTC')) ?? 'UTC';
$this->datetime = $datetime?->setTimezone($timezone);
$this->datetime = $datetime?->setTimezone(LogViewer::timezone());

$this->level = $matches['status_code'] ?? null;
$this->message = sprintf(
Expand Down
4 changes: 2 additions & 2 deletions src/Logs/HttpApacheErrorLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\CarbonInterface;
use Illuminate\Support\Carbon;
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\LogLevels\LaravelLogLevel;

class HttpApacheErrorLog extends Log
Expand All @@ -15,8 +16,7 @@ class HttpApacheErrorLog extends Log
protected function fillMatches(array $matches = []): void
{
$datetime = static::parseDateTime($matches['datetime'] ?? null);
$timezone = config('log-viewer.timezone', config('app.timezone', 'UTC')) ?? 'UTC';
$this->datetime = $datetime?->setTimezone($timezone);
$this->datetime = $datetime?->setTimezone(LogViewer::timezone());

$this->level = $matches['level'] ?? null;
$this->message = $matches['message'] ?? null;
Expand Down
4 changes: 2 additions & 2 deletions src/Logs/HttpNginxErrorLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\CarbonInterface;
use Illuminate\Support\Carbon;
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\LogLevels\NginxStatusLevel;

class HttpNginxErrorLog extends Log
Expand All @@ -15,8 +16,7 @@ class HttpNginxErrorLog extends Log
protected function fillMatches(array $matches = []): void
{
$datetime = static::parseDateTime($matches['datetime'] ?? null);
$timezone = config('log-viewer.timezone', config('app.timezone', 'UTC')) ?? 'UTC';
$this->datetime = $datetime?->setTimezone($timezone);
$this->datetime = $datetime?->setTimezone(LogViewer::timezone());

$this->level = $matches['level'] ?? null;
$this->message = $matches['errormessage'] ?? null;
Expand Down
4 changes: 1 addition & 3 deletions src/Logs/LaravelLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ protected function parseText(array &$matches = []): void

preg_match(static::regexPattern(), array_shift($firstLineSplit), $matches);

$this->datetime = Carbon::parse($matches[1])?->setTimezone(
config('log-viewer.timezone', config('app.timezone', 'UTC')) ?? 'UTC'
);
$this->datetime = Carbon::parse($matches[1])?->setTimezone(LogViewer::timezone());

// $matches[2] contains microseconds, which is already handled
// $matches[3] contains timezone offset, which is already handled
Expand Down
7 changes: 3 additions & 4 deletions src/Logs/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\CarbonInterface;
use Illuminate\Support\Carbon;
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\LogLevels\LaravelLogLevel;
use Opcodes\LogViewer\LogLevels\LevelInterface;

Expand Down Expand Up @@ -68,8 +69,7 @@ public static function matches(string $text, ?int &$timestamp = null, ?string &$
if ($result) {
try {
$datetime = static::parseDateTime($matches[static::$regexDatetimeKey] ?? null);
$timezone = config('log-viewer.timezone', config('app.timezone', 'UTC')) ?? 'UTC';
$timestamp = $datetime?->setTimezone($timezone)->timestamp;
$timestamp = $datetime?->timestamp;

$level = $matches[static::$regexLevelKey] ?? '';
} catch (\Exception $exception) {
Expand Down Expand Up @@ -108,8 +108,7 @@ protected function parseText(array &$matches = []): void
protected function fillMatches(array $matches = []): void
{
$datetime = static::parseDateTime($matches[static::$regexDatetimeKey] ?? null);
$timezone = config('log-viewer.timezone', config('app.timezone', 'UTC')) ?? 'UTC';
$this->datetime = $datetime?->setTimezone($timezone);
$this->datetime = $datetime?->setTimezone(LogViewer::timezone());

$this->level = $matches[static::$regexLevelKey] ?? null;
$this->message = trim($matches[static::$regexMessageKey] ?? null);
Expand Down
19 changes: 19 additions & 0 deletions tests/Feature/LogViewerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,22 @@
assertContains('laravel.log', $fileNames);
assertContains('other.log', $fileNames);
});

it('can get the timezone', function () {
config()->set('log-viewer.timezone', 'Europe/Vilnius');

expect(LogViewer::timezone())->toBe('Europe/Vilnius');
});

it('defaults to the app timezone', function () {
config()->set('app.timezone', 'Europe/Vilnius');

expect(LogViewer::timezone())->toBe('Europe/Vilnius');
});

it('defaults to UTC if no timezone is set anywhere', function () {
config()->set('app.timezone', null);
config()->set('log-viewer.timezone', null);

expect(LogViewer::timezone())->toBe('UTC');
});

0 comments on commit 65021bc

Please sign in to comment.