Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions src/GelfLoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/Processors/RenameIdFieldProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

Expand Down
9 changes: 7 additions & 2 deletions tests/Processors/RenameIdFieldProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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);
}
Expand All @@ -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',
],
];
}
}