Skip to content

Commit

Permalink
Add UuidProcessor and improve README
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Nov 7, 2023
1 parent fe76608 commit b66962c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
35 changes: 35 additions & 0 deletions src/UuidProcessor.php
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;
}
}
35 changes: 35 additions & 0 deletions tests/UuidProcessorTest.php
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']));
}
}

0 comments on commit b66962c

Please sign in to comment.