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

refactor how Log Viewer timezone is resolved #343

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
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');
});
Loading