diff --git a/README.md b/README.md index 1716acd..f380bdd 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,14 @@ [![Latest Stable Version](http://poser.pugx.org/alexandre-daubois/monolog-processor-collection/v/stable)](https://packagist.org/packages/alexandre-daubois/monolog-processor-collection) [![License](http://poser.pugx.org/alexandre-daubois/monolog-processor-collection/license)](https://packagist.org/packages/alexandre-daubois/monolog-processor-collection) -Monolog Processor Collection, or **MPC** for short, is a collection of useful processors for the -[Monolog](https://github.com/Seldaek/monolog) logging library. The processors -add useful information to the log records. The package is compatible with PHP 8.1+. +Welcome to the Monolog Processor Collection (MPC) - the ultimate suite of processors designed to enhance your logging +with the renowned [Monolog](https://github.com/Seldaek/monolog) library. This toolkit is meticulously crafted to +integrate seamlessly with PHP 8.1+, ensuring your logging captures the comprehensive details you need with minimal +overhead. + +MPC is engineered for developers who demand more from their logs. Whether you're tracking down elusive bugs or +monitoring live production environments, processors enrich your log entries with invaluable context, turning +ordinary logs into a rich, actionable dataset. ## Installation @@ -60,6 +65,7 @@ The package provides the following processors: - `ProtocolVersionProcessor` adds the HTTP protocol version to the log record - `ResourceUsagesProcessor` adds the resource usage to the log record as returned by [getrusage()](https://www.php.net/manual/en/function.getrusage.php) - `SapiNameProcessor` adds the name of the SAPI to the log record +- `UuidProcessor` adds a UUID v7 to the log record to track records triggered during the same request ## Integration with Symfony and MonologBundle diff --git a/composer.json b/composer.json index 3b1557f..1f2b5a6 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,8 @@ "prefer-stable": true, "require": { "php": ">=8.1", - "monolog/monolog": "^3" + "monolog/monolog": "^3", + "symfony/uid": "^6.3" }, "require-dev": { "phpunit/phpunit": "^10", diff --git a/src/UuidProcessor.php b/src/UuidProcessor.php new file mode 100644 index 0000000..cc531c8 --- /dev/null +++ b/src/UuidProcessor.php @@ -0,0 +1,35 @@ + + * + * 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; + } +} diff --git a/tests/UuidProcessorTest.php b/tests/UuidProcessorTest.php new file mode 100644 index 0000000..ea020d1 --- /dev/null +++ b/tests/UuidProcessorTest.php @@ -0,0 +1,35 @@ + + * + * 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'])); + } +}