Skip to content

Commit 65d945f

Browse files
authored
Merge pull request #343 from opcodesio/refactor/log-viewer-timezone-getter
refactor how Log Viewer timezone is resolved
2 parents dbfefa1 + 65021bc commit 65d945f

File tree

10 files changed

+46
-17
lines changed

10 files changed

+46
-17
lines changed

src/Facades/LogViewer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* @see \Opcodes\LogViewer\LogViewerService
1616
*
1717
* @method static string version()
18+
* @method static string timezone()
1819
* @method static bool assetsAreCurrent()
1920
* @method static bool supportsHostsFeature()
2021
* @method static void resolveHostsUsing(callable $callback)

src/LogViewerService.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,23 @@ class LogViewerService
1919
public static string $logFileClass = LogFile::class;
2020
public static string $logReaderClass = IndexedLogReader::class;
2121
protected ?Collection $_cachedFiles = null;
22+
protected string $_cachedTimezone;
2223
protected mixed $authCallback;
2324
protected int $maxLogSizeToDisplay = self::DEFAULT_MAX_LOG_SIZE_TO_DISPLAY;
2425
protected mixed $hostsResolver;
2526
protected string $layout = 'log-viewer::index';
2627

28+
public function timezone(): string
29+
{
30+
if (! isset($this->_cachedTimezone)) {
31+
$this->_cachedTimezone = config('log-viewer.timezone')
32+
?? config('app.timezone')
33+
?? 'UTC';
34+
}
35+
36+
return $this->_cachedTimezone;
37+
}
38+
2739
protected function getLaravelLogFilePaths(): array
2840
{
2941
// Because we'll use the base path as a parameter for `glob`, we should escape any

src/Logs/HorizonLog.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Opcodes\LogViewer\Logs;
44

55
use Opcodes\LogViewer\Exceptions\SkipLineException;
6+
use Opcodes\LogViewer\Facades\LogViewer;
67
use Opcodes\LogViewer\LogLevels\HorizonStatusLevel;
78

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

2626
$this->level = $matches['level'];
2727
$this->message = $matches['message'];

src/Logs/HorizonOldLog.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Opcodes\LogViewer\Logs;
44

5+
use Opcodes\LogViewer\Facades\LogViewer;
56
use Opcodes\LogViewer\LogLevels\HorizonStatusLevel;
67

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

2525
$this->level = $matches['level'];
2626
$this->message = $matches['message'];

src/Logs/HttpAccessLog.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Carbon\CarbonInterface;
66
use Illuminate\Support\Carbon;
7+
use Opcodes\LogViewer\Facades\LogViewer;
78
use Opcodes\LogViewer\LogLevels\HttpStatusCodeLevel;
89

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

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

4141
$this->level = $matches['status_code'] ?? null;
4242
$this->message = sprintf(

src/Logs/HttpApacheErrorLog.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Carbon\CarbonInterface;
66
use Illuminate\Support\Carbon;
7+
use Opcodes\LogViewer\Facades\LogViewer;
78
use Opcodes\LogViewer\LogLevels\LaravelLogLevel;
89

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

2121
$this->level = $matches['level'] ?? null;
2222
$this->message = $matches['message'] ?? null;

src/Logs/HttpNginxErrorLog.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Carbon\CarbonInterface;
66
use Illuminate\Support\Carbon;
7+
use Opcodes\LogViewer\Facades\LogViewer;
78
use Opcodes\LogViewer\LogLevels\NginxStatusLevel;
89

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

2121
$this->level = $matches['level'] ?? null;
2222
$this->message = $matches['errormessage'] ?? null;

src/Logs/LaravelLog.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ protected function parseText(array &$matches = []): void
3838

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

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

4543
// $matches[2] contains microseconds, which is already handled
4644
// $matches[3] contains timezone offset, which is already handled

src/Logs/Log.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Carbon\CarbonInterface;
66
use Illuminate\Support\Carbon;
7+
use Opcodes\LogViewer\Facades\LogViewer;
78
use Opcodes\LogViewer\LogLevels\LaravelLogLevel;
89
use Opcodes\LogViewer\LogLevels\LevelInterface;
910

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

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

114113
$this->level = $matches[static::$regexLevelKey] ?? null;
115114
$this->message = trim($matches[static::$regexMessageKey] ?? null);

tests/Feature/LogViewerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,22 @@
3535
assertContains('laravel.log', $fileNames);
3636
assertContains('other.log', $fileNames);
3737
});
38+
39+
it('can get the timezone', function () {
40+
config()->set('log-viewer.timezone', 'Europe/Vilnius');
41+
42+
expect(LogViewer::timezone())->toBe('Europe/Vilnius');
43+
});
44+
45+
it('defaults to the app timezone', function () {
46+
config()->set('app.timezone', 'Europe/Vilnius');
47+
48+
expect(LogViewer::timezone())->toBe('Europe/Vilnius');
49+
});
50+
51+
it('defaults to UTC if no timezone is set anywhere', function () {
52+
config()->set('app.timezone', null);
53+
config()->set('log-viewer.timezone', null);
54+
55+
expect(LogViewer::timezone())->toBe('UTC');
56+
});

0 commit comments

Comments
 (0)