-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #307 from ember-nexus/github-issue/306
Add `requestId` property to all logs to enable finding all logs made …
- Loading branch information
Showing
5 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Logger; | ||
|
||
use App\Service\RequestIdService; | ||
use Monolog\Attribute\AsMonologProcessor; | ||
use Monolog\LogRecord; | ||
use Monolog\Processor\ProcessorInterface; | ||
|
||
#[AsMonologProcessor] | ||
class RequestProcessor implements ProcessorInterface | ||
{ | ||
public function __construct( | ||
private RequestIdService $requestIdService | ||
) { | ||
} | ||
|
||
public function __invoke(LogRecord $record): LogRecord | ||
{ | ||
$record->extra['requestId'] = $this->requestIdService->getRequestId()->toString(); | ||
|
||
return $record; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Service; | ||
|
||
use Ramsey\Uuid\Uuid; | ||
use Ramsey\Uuid\UuidInterface; | ||
|
||
class RequestIdService | ||
{ | ||
private UuidInterface $requestId; | ||
|
||
public function __construct() | ||
{ | ||
$this->requestId = Uuid::uuid4(); | ||
} | ||
|
||
public function getRequestId(): UuidInterface | ||
{ | ||
return $this->requestId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\tests\UnitTests\Logger; | ||
|
||
use App\Logger\RequestProcessor; | ||
use App\Service\RequestIdService; | ||
use DateTimeImmutable; | ||
use Monolog\Level; | ||
use Monolog\LogRecord; | ||
use PHPUnit\Framework\TestCase; | ||
use Ramsey\Uuid\Uuid; | ||
|
||
class RequestProcessorTest extends TestCase | ||
{ | ||
public function testRequestProcessor(): void | ||
{ | ||
$requestIdService = $this->createMock(RequestIdService::class); | ||
$requestIdService->method('getRequestId')->willReturn( | ||
Uuid::fromString('3950e94a-2cd0-43ef-ac5a-8e116a4263c8') | ||
); | ||
|
||
$requestProcessor = new RequestProcessor($requestIdService); | ||
|
||
$logRecord = new LogRecord( | ||
new DateTimeImmutable(), | ||
'channel', | ||
Level::Debug, | ||
'message' | ||
); | ||
|
||
$newLogRecord = $requestProcessor->__invoke($logRecord); | ||
$this->assertSame($logRecord, $newLogRecord); | ||
$this->assertArrayHasKey('requestId', $logRecord->extra); | ||
$this->assertSame('3950e94a-2cd0-43ef-ac5a-8e116a4263c8', $logRecord->extra['requestId']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\tests\UnitTests\Service; | ||
|
||
use App\Service\RequestIdService; | ||
use PHPUnit\Framework\TestCase; | ||
use Ramsey\Uuid\UuidInterface; | ||
|
||
class RequestIdServiceTest extends TestCase | ||
{ | ||
public function testValidateTypeFromBody(): void | ||
{ | ||
$requestIdService = new RequestIdService(); | ||
$this->assertInstanceOf(UuidInterface::class, $requestIdService->getRequestId()); | ||
} | ||
} |