diff --git a/src/Facades/LogViewer.php b/src/Facades/LogViewer.php index 823db033..2c46ca1f 100644 --- a/src/Facades/LogViewer.php +++ b/src/Facades/LogViewer.php @@ -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) diff --git a/src/LogViewerService.php b/src/LogViewerService.php index 5592a1ac..f3c44239 100755 --- a/src/LogViewerService.php +++ b/src/LogViewerService.php @@ -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 diff --git a/src/Logs/HorizonLog.php b/src/Logs/HorizonLog.php index 080a5ad4..18261120 100644 --- a/src/Logs/HorizonLog.php +++ b/src/Logs/HorizonLog.php @@ -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 @@ -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']; diff --git a/src/Logs/HorizonOldLog.php b/src/Logs/HorizonOldLog.php index a714e4f2..9a4873a3 100644 --- a/src/Logs/HorizonOldLog.php +++ b/src/Logs/HorizonOldLog.php @@ -2,6 +2,7 @@ namespace Opcodes\LogViewer\Logs; +use Opcodes\LogViewer\Facades\LogViewer; use Opcodes\LogViewer\LogLevels\HorizonStatusLevel; class HorizonOldLog extends Log @@ -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']; diff --git a/src/Logs/HttpAccessLog.php b/src/Logs/HttpAccessLog.php index dc9f8765..921c1427 100644 --- a/src/Logs/HttpAccessLog.php +++ b/src/Logs/HttpAccessLog.php @@ -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 @@ -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( diff --git a/src/Logs/HttpApacheErrorLog.php b/src/Logs/HttpApacheErrorLog.php index c4932541..be1fdd9b 100644 --- a/src/Logs/HttpApacheErrorLog.php +++ b/src/Logs/HttpApacheErrorLog.php @@ -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 @@ -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; diff --git a/src/Logs/HttpNginxErrorLog.php b/src/Logs/HttpNginxErrorLog.php index 4e4e1fc6..e980eb64 100644 --- a/src/Logs/HttpNginxErrorLog.php +++ b/src/Logs/HttpNginxErrorLog.php @@ -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 @@ -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; diff --git a/src/Logs/LaravelLog.php b/src/Logs/LaravelLog.php index cfe60deb..8c89dc88 100644 --- a/src/Logs/LaravelLog.php +++ b/src/Logs/LaravelLog.php @@ -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 diff --git a/src/Logs/Log.php b/src/Logs/Log.php index af49c8b3..8e8010bd 100644 --- a/src/Logs/Log.php +++ b/src/Logs/Log.php @@ -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; @@ -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) { @@ -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); diff --git a/tests/Feature/LogViewerTest.php b/tests/Feature/LogViewerTest.php index b9bf1943..e7cefa01 100644 --- a/tests/Feature/LogViewerTest.php +++ b/tests/Feature/LogViewerTest.php @@ -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'); +});