Skip to content

Commit

Permalink
Merge pull request #221 from opcodesio/feature/custom-timezone-for-vi…
Browse files Browse the repository at this point in the history
…ewing-logs

add the ability to set a time zone for Log Viewer UI
  • Loading branch information
arukompas authored Mar 31, 2023
2 parents 938c314 + fb0b02a commit eed3745
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opcodesio/log-viewer",
"version": "v2.3.2",
"version": "v2.4.0",
"description": "Fast and easy-to-use log viewer for your Laravel application",
"keywords": [
"arukompas",
Expand Down
11 changes: 11 additions & 0 deletions config/log-viewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@

'back_to_system_label' => null, // Displayed by default: "Back to {{ app.name }}"

/*
|--------------------------------------------------------------------------
| Log Viewer time zone.
|--------------------------------------------------------------------------
| The time zone in which to display the times in the UI. Defaults to
| the application's timezone defined in config/app.php.
|
*/

'timezone' => null,

/*
|--------------------------------------------------------------------------
| Log Viewer route middleware.
Expand Down
4 changes: 3 additions & 1 deletion src/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public function __construct(
$firstLineSplit = str_split($firstLine, 1000);
preg_match(LogViewer::laravelRegexPattern(), array_shift($firstLineSplit), $matches);

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

// $matches[2] contains microseconds, which is already handled
// $matches[3] contains timezone offset, which is already handled
Expand Down
11 changes: 11 additions & 0 deletions tests/Unit/LogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,14 @@
assertEquals('production', $log->environment);
assertEquals('', $log->fullText);
});

it('can set a custom timezone of the log entry', function () {
$text = '[2022-11-07 17:51:33] production.ERROR: test message';
config(['log-viewer.timezone' => $tz = 'Europe/Vilnius']);

$log = new Log(0, $text, 'laravel.log', 0);

assertEquals($tz, $log->time->timezoneName);
$expectedTime = \Carbon\Carbon::parse('2022-11-07 17:51:33', 'UTC')->tz($tz)->toDateTimeString();
assertEquals($expectedTime, $log->time->toDateTimeString());
});

0 comments on commit eed3745

Please sign in to comment.