Skip to content

Commit

Permalink
Merge pull request #307 from ember-nexus/github-issue/306
Browse files Browse the repository at this point in the history
Add `requestId` property to all logs to enable finding all logs made …
  • Loading branch information
Syndesi authored Apr 27, 2024
2 parents 9b08309 + ec2a973 commit 2f2f788
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
### Fixed
- Fix wrong license identifier in Dockerfile, closes #302.
### Added
- Add `requestId` property to all logs to enable finding all logs made during a single request, closes #306.
### Changed
- Change naming of internal variables to replace `Uuid` with `Id`, because the variables are already type hinted and
`Id` emerged as a better convention in the ecosystem. Closes #292.
Expand Down
26 changes: 26 additions & 0 deletions src/Logger/RequestProcessor.php
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;
}
}
23 changes: 23 additions & 0 deletions src/Service/RequestIdService.php
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;
}
}
38 changes: 38 additions & 0 deletions tests/UnitTests/Logger/RequestProcessorTest.php
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']);
}
}
18 changes: 18 additions & 0 deletions tests/UnitTests/Service/RequestIdServiceTest.php
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());
}
}

0 comments on commit 2f2f788

Please sign in to comment.