Skip to content

Commit

Permalink
ShipSaaS Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
sethsandaru committed Oct 25, 2023
1 parent 4bd0967 commit 77ce319
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Handlers/UniqueRequestIdFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
110 changes: 110 additions & 0 deletions tests/Integration/ShipSaasLoggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace ShipSaasUniqueRequestLogger\Tests\Integration;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use ShipSaasUniqueRequestLogger\Constants\GlobalConstants;
use ShipSaasUniqueRequestLogger\Tests\TestCase;

class ShipSaasLoggerTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->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';
});
}
}
77 changes: 77 additions & 0 deletions tests/Unit/EventHandlers/GenerateUniqueRequestIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace ShipSaasUniqueRequestLogger\Tests\Unit\EventHandlers;

use Illuminate\Http\Request;
use Illuminate\Routing\Events\Routing;
use Illuminate\Support\Str;
use ShipSaasUniqueRequestLogger\Constants\GlobalConstants;
use ShipSaasUniqueRequestLogger\EventHandlers\GenerateUniqueRequestId;
use ShipSaasUniqueRequestLogger\Tests\TestCase;

class GenerateUniqueRequestIdTest extends TestCase
{
public function testHandlerGeneratesAndSetsTheUniqueRequestIdUsingUuid()
{
$request = $this->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());
}
}
25 changes: 25 additions & 0 deletions tests/Unit/Helpers/HelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace ShipSaasUniqueRequestLogger\Tests\Unit\Helpers;

use ShipSaasUniqueRequestLogger\Helpers\Helpers;
use ShipSaasUniqueRequestLogger\Tests\TestCase;

class HelpersTest extends TestCase
{
public function testGetRequestIdReturnsRequestId()
{
request()->setRequestId('hehe');

$rqId = Helpers::getUniqueRequestId();

$this->assertSame('hehe', $rqId);
}

public function testGetRequestIdReturnsNull()
{
$rqId = Helpers::getUniqueRequestId();

$this->assertNull($rqId);
}
}

0 comments on commit 77ce319

Please sign in to comment.