From 77ce319ba31f9c853ad034db7b08f694a368c58f Mon Sep 17 00:00:00 2001 From: Seth Phat Date: Wed, 25 Oct 2023 19:37:14 +0700 Subject: [PATCH] ShipSaaS Logger --- src/Handlers/UniqueRequestIdFileHandler.php | 2 +- tests/Integration/ShipSaasLoggerTest.php | 110 ++++++++++++++++++ .../GenerateUniqueRequestIdTest.php | 77 ++++++++++++ tests/Unit/Helpers/HelpersTest.php | 25 ++++ 4 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 tests/Integration/ShipSaasLoggerTest.php create mode 100644 tests/Unit/EventHandlers/GenerateUniqueRequestIdTest.php create mode 100644 tests/Unit/Helpers/HelpersTest.php diff --git a/src/Handlers/UniqueRequestIdFileHandler.php b/src/Handlers/UniqueRequestIdFileHandler.php index d77ed90..4402a73 100644 --- a/src/Handlers/UniqueRequestIdFileHandler.php +++ b/src/Handlers/UniqueRequestIdFileHandler.php @@ -30,7 +30,7 @@ public function __construct( parent::__construct($filename, $level, $bubble, $filePermission, $useLocking); } - public function getFullFilePath(string $requestId): string + protected function getFullFilePath(string $requestId): string { $fileInfo = pathinfo($this->filename); $fileName = $requestId . '.' . ($fileInfo['extension'] ?? '.log'); diff --git a/tests/Integration/ShipSaasLoggerTest.php b/tests/Integration/ShipSaasLoggerTest.php new file mode 100644 index 0000000..0f7ff1b --- /dev/null +++ b/tests/Integration/ShipSaasLoggerTest.php @@ -0,0 +1,110 @@ +createFakeApp(); + } + + public function testWriteLogShouldContainsRequestIdNormalView() + { + $res = $this->json('GET', 'write-logs') + ->assertOk() + ->assertSee('ok') + ->assertHeader(GlobalConstants::REQUEST_ID_HEADER); + + $headerValue = $res->headers->get(GlobalConstants::REQUEST_ID_HEADER); + + // header is valid + $this->assertNotNull($headerValue); + $this->assertTrue(Str::isUlid($headerValue)); + + // log file assertions + $filePath = __DIR__ . "/logs/$headerValue.log"; + $this->assertFileExists($filePath); + + // read the logs + $logs = file_get_contents($filePath); + $lines = explode("\n", $logs); + + // we write 2 lines, but the last \n count too => 3 + $this->assertCount(3, $lines); + + $this->assertStringContainsString('{"requestId":"'.$headerValue.'"}', $lines[0]); + $this->assertStringContainsString('{"requestId":"'.$headerValue.'"}', $lines[1]); + } + + public function testWriteLogShouldContainsRequestIdJsonView() + { + config([ + 'logging.channels.shipsaas-logger.use_json_format' => true, + ]); + + $res = $this->json('GET', 'write-logs') + ->assertOk() + ->assertSee('ok') + ->assertHeader(GlobalConstants::REQUEST_ID_HEADER); + + $headerValue = $res->headers->get(GlobalConstants::REQUEST_ID_HEADER); + + // header is valid + $this->assertNotNull($headerValue); + $this->assertTrue(Str::isUlid($headerValue)); + + // log file assertions + $filePath = __DIR__ . "/logs/$headerValue.log"; + $this->assertFileExists($filePath); + + // read the logs + $logs = file_get_contents($filePath); + $lines = explode("\n", $logs); + + // we write 2 lines, but the last \n count too => 3 + $this->assertCount(3, $lines); + + $this->assertJson($lines[0]); + $this->assertJson($lines[1]); + + $line0Json = json_decode($lines[0], true); + $line1Json = json_decode($lines[1], true); + + $this->assertSame($headerValue, $line0Json['extra']['requestId']); + $this->assertSame($headerValue, $line1Json['extra']['requestId']); + } + + private function createFakeApp(): void + { + config([ + 'logging.default' => 'shipsaas-logger', + 'logging.channels.shipsaas-logger' => [ + 'driver' => 'shipsaas-logger', + 'path' => __DIR__ . '/logs/hehe.log', + 'default_log_file' => __DIR__ . '/logs/hehe.log', + 'id-type' => 'ulid', + 'use_json_format' => false, + ], + ]); + + Route::get('write-logs', function () { + Log::info('Test info log', [ + 'id' => 'this-is-id', + ]); + + Log::warning('Test warning pro', [ + 'id' => 'this-is-id-warning', + ]); + + return 'ok'; + }); + } +} diff --git a/tests/Unit/EventHandlers/GenerateUniqueRequestIdTest.php b/tests/Unit/EventHandlers/GenerateUniqueRequestIdTest.php new file mode 100644 index 0000000..e846630 --- /dev/null +++ b/tests/Unit/EventHandlers/GenerateUniqueRequestIdTest.php @@ -0,0 +1,77 @@ +app->make(Request::class); + + config([ + 'logging.channels.shipsaas-logger.id-type' => 'uuid', + ]); + + $event = new Routing($request); + + $handler = new GenerateUniqueRequestId(); + $handler->handle($event); + + $this->assertNotNull($request->getRequestId()); + $this->assertTrue(Str::isUuid($request->getRequestId())); + } + + public function testHandlerGeneratesAndSetsTheUniqueRequestIdUsingOrderedUuid() + { + $request = $this->app->make(Request::class); + + config([ + 'logging.channels.shipsaas-logger.id-type' => 'orderedUuid', + ]); + + $event = new Routing($request); + + $handler = new GenerateUniqueRequestId(); + $handler->handle($event); + + $this->assertNotNull($request->getRequestId()); + $this->assertTrue(Str::isUuid($request->getRequestId())); + } + + public function testHandlerGeneratesAndSetsTheUniqueRequestIdUsingUlid() + { + $request = $this->app->make(Request::class); + + config([ + 'logging.channels.shipsaas-logger.id-type' => 'ulid', + ]); + + $event = new Routing($request); + + $handler = new GenerateUniqueRequestId(); + $handler->handle($event); + + $this->assertNotNull($request->getRequestId()); + $this->assertTrue(Str::isUlid($request->getRequestId())); + } + + public function testHandlerSetsTheUniqueRequestIdFromHeader() + { + $request = $this->app->make(Request::class); + $request->headers->set(GlobalConstants::REQUEST_ID_HEADER, 'test'); + + $event = new Routing($request); + + $handler = new GenerateUniqueRequestId(); + $handler->handle($event); + + $this->assertSame('test', $request->getRequestId()); + } +} diff --git a/tests/Unit/Helpers/HelpersTest.php b/tests/Unit/Helpers/HelpersTest.php new file mode 100644 index 0000000..f06362c --- /dev/null +++ b/tests/Unit/Helpers/HelpersTest.php @@ -0,0 +1,25 @@ +setRequestId('hehe'); + + $rqId = Helpers::getUniqueRequestId(); + + $this->assertSame('hehe', $rqId); + } + + public function testGetRequestIdReturnsNull() + { + $rqId = Helpers::getUniqueRequestId(); + + $this->assertNull($rqId); + } +}