diff --git a/.gitignore b/.gitignore index 18546609..59ab0798 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,7 @@ /composer.phar /composer.lock /phpunit.xml +/.vscode .DS_Store Thumbs.db +.phpunit.result.cache diff --git a/constants.php b/constants.php index 7fe5af83..bc85e3f1 100644 --- a/constants.php +++ b/constants.php @@ -1,21 +1,34 @@ setDatetime($this->extractDatetime($header)); + $this->setDatetime(...$this->extractDatetime($header)); $header = $this->cleanHeader($header); @@ -115,7 +118,7 @@ private function setContext(array $context) */ private function setEnv($env) { - $this->env = head(explode('.', $env)); + $this->env = $env; return $this; } @@ -123,13 +126,14 @@ private function setEnv($env) /** * Set the entry date time. * + * @param string $format * @param string $datetime * * @return \Arcanedev\LogViewer\Entities\LogEntry */ - private function setDatetime($datetime) + private function setDatetime($format, $datetime) { - $this->datetime = Carbon::createFromFormat('Y-m-d H:i:s', $datetime); + $this->datetime = Carbon::createFromFormat($format, $datetime); return $this; } @@ -155,7 +159,7 @@ private function setStack($stack) */ public function level() { - return $this->icon()->toHtml().' '.$this->name(); + return $this->icon()->toHtml() . ' ' . $this->name(); } /** @@ -279,7 +283,7 @@ public function hasStack() */ public function hasContext() { - return ! empty($this->context); + return !empty($this->context); } /* ----------------------------------------------------------------- @@ -297,17 +301,18 @@ public function hasContext() private function cleanHeader($header) { // REMOVE THE DATE - $header = preg_replace('/\['.REGEX_DATETIME_PATTERN.'\][ ]/', '', $header); + $header = preg_replace('/\[' . REGEX_DATETIME_PATTERN . '\]/', '', $header); // EXTRACT ENV - if (preg_match('/^[a-z]+.[A-Z]+:/', $header, $out)) { - $this->setEnv($out[0]); - $header = trim(str_replace($out[0], '', $header)); + $regex = '/.* ([a-z]+)\.[A-Z]+:/'; + if (preg_match_all($regex, $header, $out)) { + $this->setEnv($out[1][0] ?? null); + $header = preg_replace($regex, '', $header); } // EXTRACT CONTEXT (Regex from https://stackoverflow.com/a/21995025) preg_match_all('/{(?:[^{}]|(?R))*}/x', $header, $out); - if (isset($out[0][0]) && ! is_null($context = json_decode($out[0][0], true))) { + if (isset($out[0][0]) && !is_null($context = json_decode($out[0][0], true))) { $header = str_replace($out[0][0], '', $header); $this->setContext($context); } @@ -320,10 +325,18 @@ private function cleanHeader($header) * * @param string $header * - * @return string + * @return array */ private function extractDatetime($header) { - return preg_replace('/^\[('.REGEX_DATETIME_PATTERN.')\].*/', '$1', $header); + if (preg_match_all('/^\[(' . REGEX_DATETIME_PATTERN . ')\].*/', $header, $matches)) { + $separator = ($matches[2][0] ?? null) === 'T' ? '\T' : ' '; + $ms = ($matches[3][0] ?? null) ? '.u' : ''; + $tz = ($matches[4][0] ?? null) ? 'P' : ''; + $format = "Y-m-d{$separator}H:i:s{$ms}{$tz}"; + $datetime = $matches[1][0]; + return [$format, $datetime]; + } + return []; } } diff --git a/src/Helpers/LogParser.php b/src/Helpers/LogParser.php index 5c07b2ea..9f31e8a2 100644 --- a/src/Helpers/LogParser.php +++ b/src/Helpers/LogParser.php @@ -1,4 +1,6 @@ -logs); - static::assertSame(2, $this->logs->count()); - static::assertSame(16, $this->logs->total()); + static::assertCount(3, $this->logs); + static::assertSame(3, $this->logs->count()); + static::assertSame(24, $this->logs->total()); foreach ($this->logs as $date => $log) { /** @var \Arcanedev\LogViewer\Entities\Log $log */ @@ -137,7 +137,7 @@ public function it_can_get_log_tree() { $tree = $this->logs->tree(); - static::assertCount(2, $tree); + static::assertCount(3, $tree); foreach ($tree as $date => $levels) { static::assertDate($date); diff --git a/tests/LogViewerTest.php b/tests/LogViewerTest.php index f31a8c28..262562d0 100644 --- a/tests/LogViewerTest.php +++ b/tests/LogViewerTest.php @@ -58,20 +58,20 @@ public function it_can_be_instantiated_with_helper() /** @test */ public function it_can_get_logs_count() { - static::assertSame(2, $this->logViewer->count()); + static::assertSame(3, $this->logViewer->count()); } /** @test */ public function it_can_get_entries_total() { - static::assertSame(16, $this->logViewer->total()); + static::assertSame(24, $this->logViewer->total()); } /** @test */ public function it_can_get_entries_total_by_level() { foreach (self::$logLevels as $level) { - static::assertSame(2, $this->logViewer->total($level)); + static::assertSame(3, $this->logViewer->total($level)); } } @@ -80,8 +80,8 @@ public function it_can_get_all_logs() { $logs = $this->logViewer->all(); - static::assertCount(2, $logs); - static::assertSame(2, $logs->count()); + static::assertCount(3, $logs); + static::assertSame(3, $logs->count()); foreach ($logs as $log) { /** @var Log $log */ @@ -101,7 +101,7 @@ public function it_can_paginate_all_logs() static::assertInstanceOf(\Illuminate\Pagination\LengthAwarePaginator::class, $logs); static::assertSame(30, $logs->perPage()); - static::assertSame(2, $logs->total()); + static::assertSame(3, $logs->total()); static::assertSame(1, $logs->lastPage()); static::assertSame(1, $logs->currentPage()); } @@ -158,7 +158,7 @@ public function it_can_get_log_dates() { $dates = $this->logViewer->dates(); - static::assertCount(2, $dates); + static::assertCount(3, $dates); static::assertDates($dates); } @@ -167,7 +167,7 @@ public function it_can_get_log_files() { $files = $this->logViewer->files(); - static::assertCount(2, $files); + static::assertCount(3, $files); foreach ($files as $file) { static::assertFileExists($file); } @@ -228,7 +228,7 @@ public function it_can_get_tree() { $tree = $this->logViewer->tree(); - static::assertCount(2, $tree); + static::assertCount(3, $tree); foreach ($tree as $date => $counters) { static::assertDate($date); @@ -254,7 +254,7 @@ public function it_can_get_translated_menu() $this->app->setLocale($locale); $menu = $this->logViewer->menu(); - static::assertCount(2, $menu); + static::assertCount(3, $menu); foreach ($menu as $date => $counters) { static::assertDate($date); diff --git a/tests/Tables/StatsTableTest.php b/tests/Tables/StatsTableTest.php index af96f64e..a09099b8 100644 --- a/tests/Tables/StatsTableTest.php +++ b/tests/Tables/StatsTableTest.php @@ -195,7 +195,7 @@ protected static function assertTableRows(TableContract $table) protected static function assertTableFooter(TableContract $table) { foreach ($table->footer() as $key => $value) { - self::assertEquals($key === 'all' ? 16 : 2, $value); + self::assertEquals($key === 'all' ? 24 : 3, $value); } } diff --git a/tests/Utilities/FactoryTest.php b/tests/Utilities/FactoryTest.php index 458cd875..7a02bafc 100644 --- a/tests/Utilities/FactoryTest.php +++ b/tests/Utilities/FactoryTest.php @@ -90,7 +90,7 @@ public function it_can_get_dates() { $dates = $this->logFactory->dates(); - static::assertCount(2, $dates); + static::assertCount(3, $dates); static::assertDates($dates); } @@ -100,8 +100,8 @@ public function it_can_get_all_logs() $logs = $this->logFactory->all(); static::assertInstanceOf(\Arcanedev\LogViewer\Entities\LogCollection::class, $logs); - static::assertCount(2, $logs); - static::assertSame(2, $logs->count()); + static::assertCount(3, $logs); + static::assertSame(3, $logs->count()); } /** @test */ @@ -111,7 +111,7 @@ public function it_can_paginate_all_logs() static::assertInstanceOf(\Illuminate\Pagination\LengthAwarePaginator::class, $logs); static::assertSame(30, $logs->perPage()); - static::assertSame(2, $logs->total()); + static::assertSame(3, $logs->total()); static::assertSame(1, $logs->lastPage()); static::assertSame(1, $logs->currentPage()); } @@ -119,7 +119,7 @@ public function it_can_paginate_all_logs() /** @test */ public function it_can_get_count() { - static::assertSame(2, $this->logFactory->count()); + static::assertSame(3, $this->logFactory->count()); } /** @test */ @@ -139,14 +139,14 @@ public function it_can_can_set_custom_path() /** @test */ public function it_can_get_total() { - static::assertSame(16, $this->logFactory->total()); + static::assertSame(24, $this->logFactory->total()); } /** @test */ public function it_can_get_total_by_level() { foreach (self::$logLevels as $level) { - static::assertSame(2, $this->logFactory->total($level)); + static::assertSame(3, $this->logFactory->total($level)); } } @@ -168,6 +168,17 @@ public function it_can_get_translated_tree() $this->app->setLocale('fr'); $expected = [ + '2015-01-03' => [ + 'all' => ['name' => 'Tous', 'count' => 8], + 'emergency' => ['name' => 'Urgence', 'count' => 1], + 'alert' => ['name' => 'Alerte', 'count' => 1], + 'critical' => ['name' => 'Critique', 'count' => 1], + 'error' => ['name' => 'Erreur', 'count' => 1], + 'warning' => ['name' => 'Avertissement', 'count' => 1], + 'notice' => ['name' => 'Notice', 'count' => 1], + 'info' => ['name' => 'Info', 'count' => 1], + 'debug' => ['name' => 'Debug', 'count' => 1], + ], '2015-01-02' => [ 'all' => ['name' => 'Tous', 'count' => 8], 'emergency' => ['name' => 'Urgence', 'count' => 1], diff --git a/tests/Utilities/FilesystemTest.php b/tests/Utilities/FilesystemTest.php index 9020ff8f..e919ad77 100644 --- a/tests/Utilities/FilesystemTest.php +++ b/tests/Utilities/FilesystemTest.php @@ -61,7 +61,7 @@ public function it_can_get_filesystem_instance() /** @test */ public function it_can_get_all_valid_log_files() { - static::assertCount(2, $this->filesystem->logs()); + static::assertCount(3, $this->filesystem->logs()); } /** @test */ @@ -79,7 +79,7 @@ public function it_can_get_all_log_files() { $files = $this->filesystem->all(); - static::assertCount(5, $files); + static::assertCount(6, $files); foreach ($files as $file) { static::assertStringEndsWith('.log', $file); @@ -123,7 +123,7 @@ public function it_can_get_files() { $files = $this->filesystem->logs(); - static::assertCount(2, $files); + static::assertCount(3, $files); foreach ($files as $file) { static::assertFileExists($file); diff --git a/tests/fixtures/logs/laravel-2015-01-03.log b/tests/fixtures/logs/laravel-2015-01-03.log new file mode 100644 index 00000000..8538e11d --- /dev/null +++ b/tests/fixtures/logs/laravel-2015-01-03.log @@ -0,0 +1,41 @@ +[2015-01-03T00:00:00+00:00][uuid-uuid-uuid-uuid] test.EMERGENCY: This is an emergency log. +Stack trace: +#0 Trace 1 +#1 Trace 2 +#2 Trace 3 +#3 Trace 4 +#4 Trace 5 +[2015-01-03T00:00:01+00:00][uuid-uuid-uuid-uuid] test.ALERT: This is an alert log. +Stack trace: +#0 Trace 1 +#1 Trace 2 +#2 Trace 3 +#3 Trace 4 +#4 Trace 5 +[2015-01-03T00:00:02+00:00][uuid-uuid-uuid-uuid] test.CRITICAL: This is an critical log. +Stack trace: +#0 Trace 1 +#1 Trace 2 +#2 Trace 3 +#3 Trace 4 +#4 Trace 5 +[2015-01-03T00:00:03+00:00][uuid-uuid-uuid-uuid] test.ERROR: This is an error log. +Stack trace: +#0 Trace 1 +#1 Trace 2 +#2 Trace 3 +#3 Trace 4 +#4 Trace 5 +[2015-01-03T00:00:04+00:00][uuid-uuid-uuid-uuid] test.WARNING: This is an warning log. +Stack trace: +#0 Trace 1 +#1 Trace 2 +#2 Trace 3 +#3 Trace 4 +#4 Trace 5 +[2015-01-03T00:00:05+00:00][uuid-uuid-uuid-uuid] test.NOTICE: This is an notice log. +Only a notice log +[2015-01-03T00:00:06+00:00][uuid-uuid-uuid-uuid] test.INFO: This is an info log. +Only a info log +[2015-01-03T00:00:07.123456+00:00][uuid-uuid-uuid-uuid] test.DEBUG: This is an debug log. +Only a debug log