-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UuidProcessor and improve README
- Loading branch information
1 parent
fe76608
commit b66962c
Showing
4 changed files
with
81 additions
and
4 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
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,35 @@ | ||
<?php | ||
|
||
/* | ||
* (c) Alexandre Daubois <alex.daubois@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace MonologProcessorCollection; | ||
|
||
use Monolog\LogRecord; | ||
use Monolog\ResettableInterface; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
/** | ||
* Generates and add a UUID to the log record for the current request. | ||
* It uses a timestampable UUID v7. | ||
*/ | ||
final class UuidProcessor extends AbstractThresholdProcessor implements ResettableInterface | ||
{ | ||
private static ?Uuid $uuid = null; | ||
|
||
protected function process(LogRecord $record): LogRecord | ||
{ | ||
$record['extra']['uuid'] = (self::$uuid ??= Uuid::v7())->toRfc4122(); | ||
|
||
return $record; | ||
} | ||
|
||
public function reset(): void | ||
{ | ||
self::$uuid = null; | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
/* | ||
* (c) Alexandre Daubois <alex.daubois@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
|
||
use Monolog\Handler\TestHandler; | ||
use Monolog\Level; | ||
use MonologProcessorCollection\Tests\AbstractProcessorTestCase; | ||
use MonologProcessorCollection\UuidProcessor; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
#[CoversClass(UuidProcessor::class)] | ||
class UuidProcessorTest extends AbstractProcessorTestCase | ||
{ | ||
public function testItWorks(): void | ||
{ | ||
$processor = new UuidProcessor(); | ||
|
||
$handler = new TestHandler(); | ||
$handler->pushProcessor($processor); | ||
$handler->handle($this->createRecord(Level::Notice)); | ||
|
||
$this->assertTrue($handler->hasNoticeRecords()); | ||
$record = $handler->getRecords()[0]; | ||
|
||
$this->assertArrayHasKey('uuid', $record->extra); | ||
$this->assertTrue(Uuid::isValid($record->extra['uuid'])); | ||
} | ||
} |