From f75f5bbab494d1f52073e211a7d23ac27ec3e16d Mon Sep 17 00:00:00 2001 From: Mikhail Volkov Date: Mon, 29 Jan 2024 14:42:06 +0300 Subject: [PATCH 1/2] Laravel 10 compatible processor configurations --- src/GelfLoggerFactory.php | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) 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 From 49a56459d8e7f067fc905bba641d298c80a3311b Mon Sep 17 00:00:00 2001 From: Mikhail Volkov Date: Mon, 29 Jan 2024 17:26:14 +0300 Subject: [PATCH 2/2] Adding RenameIdFieldProcessor change fieldname feature --- src/Processors/RenameIdFieldProcessor.php | 6 +++++- tests/Processors/RenameIdFieldProcessorTest.php | 9 +++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) 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', + ], ]; } }