diff --git a/src/GelfLoggerFactory.php b/src/GelfLoggerFactory.php index 04c9a79..7ca6519 100644 --- a/src/GelfLoggerFactory.php +++ b/src/GelfLoggerFactory.php @@ -51,8 +51,8 @@ public function __invoke(array $config): Logger ) ); - foreach ($this->parseProcessors($config) as $processor) { - $handler->pushProcessor(new $processor); + foreach ($this->prepareProcessors($config) as $processor) { + $handler->pushProcessor($processor); } return new Logger($this->parseChannel($config), [$handler]); @@ -125,17 +125,11 @@ protected function sslOptions(?array $sslConfig = null): SslOptions return $sslOptions; } - protected function parseProcessors(array $config): array + protected function prepareProcessors(array $config): array { - $processors = []; - - if (isset($config['processors']) && is_array($config['processors'])) { - foreach ($config['processors'] as $processor) { - $processors[] = $processor; - } - } - - return $processors; + return collect($config['processors'] ?? []) + ->map(fn ($processor) => $this->app->make($processor['processor'] ?? $processor, $processor['with'] ?? [])) + ->toArray(); } protected function getFallbackChannelName(): string diff --git a/src/Processors/RenameIdFieldProcessor.php b/src/Processors/RenameIdFieldProcessor.php index 5e4b4f4..ef79ace 100644 --- a/src/Processors/RenameIdFieldProcessor.php +++ b/src/Processors/RenameIdFieldProcessor.php @@ -12,12 +12,16 @@ class RenameIdFieldProcessor implements ProcessorInterface * * @see https://github.com/hedii/laravel-gelf-logger/issues/33 */ + public function __construct(private readonly string $fieldField = '_id') + { + } + public function __invoke(LogRecord $record): LogRecord { $context = $record->context; if (array_key_exists('id', $context)) { - $context['_id'] = $context['id']; + $context[$this->fieldField] = $context['id']; unset($context['id']); } diff --git a/tests/Processors/RenameIdFieldProcessorTest.php b/tests/Processors/RenameIdFieldProcessorTest.php index 8500151..b2297d6 100644 --- a/tests/Processors/RenameIdFieldProcessorTest.php +++ b/tests/Processors/RenameIdFieldProcessorTest.php @@ -14,7 +14,7 @@ class RenameIdFieldProcessorTest extends TestCase * @test * @dataProvider dataProvider */ - public function it_should_rename_id_field(array $payloadContext, array $expected): void + public function it_should_rename_id_field(array $payloadContext, array $expected, string $fieldName = '_id'): void { $payload = new LogRecord( datetime: new DateTimeImmutable(), @@ -24,7 +24,7 @@ public function it_should_rename_id_field(array $payloadContext, array $expected context: $payloadContext ); - $processor = new RenameIdFieldProcessor(); + $processor = new RenameIdFieldProcessor($fieldName); $this->assertSame($expected, $processor($payload)->context); } @@ -48,6 +48,11 @@ public static function dataProvider(): array ['_id' => 'bar', 'field1' => 'value1'], ['_id' => 'bar', 'field1' => 'value1'], ], + 'having custom id filedName configuration' => [ + ['id' => 'bar'], + ['_other_key' => 'bar'], + '_other_key', + ], ]; } }