Skip to content

Commit

Permalink
Merge pull request #42 from patriziotomato/upgrade-processors
Browse files Browse the repository at this point in the history
Upgrade processors to Monolog 3 and fixe issue in RenameIdFieldProcessor
  • Loading branch information
hedii authored Feb 19, 2023
2 parents 2a4f42d + 986d644 commit 47d82c3
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 28 deletions.
12 changes: 8 additions & 4 deletions src/Processors/NullStringProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

namespace Hedii\LaravelGelfLogger\Processors;

use Monolog\LogRecord;

class NullStringProcessor
{
/**
* Transform a "NULL" string record into a null value.
*/
public function __invoke(array $record): array
public function __invoke(LogRecord $record): LogRecord
{
foreach ($record['context'] as $key => $value) {
$context = $record->context;

foreach ($context as $key => $value) {
if (is_string($value) && strtoupper($value) === 'NULL') {
$record['context'][$key] = null;
$context[$key] = null;
}
}

return $record;
return $record->with(context: $context);
}
}
15 changes: 8 additions & 7 deletions src/Processors/RenameIdFieldProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

namespace Hedii\LaravelGelfLogger\Processors;

use Monolog\LogRecord;

class RenameIdFieldProcessor
{
/**
* Rename "id" field to "_id" (additional field 'id' is not allowed).
*
* @see https://github.com/hedii/laravel-gelf-logger/issues/33
*/
public function __invoke(array $record): array
public function __invoke(LogRecord $record): LogRecord
{
foreach ($record['context'] as $key => $value) {
if ($key === 'id' && ! array_key_exists('_id', $record['context'])) {
unset($record['context']['id']);
$context = $record->context;

$record['context']['_id'] = $value;
}
if (array_key_exists('id', $context)) {
$context['_id'] = $context['id'];
unset($context['id']);
}

return $record;
return $record->with(context: $context);
}
}
28 changes: 17 additions & 11 deletions tests/Processors/NullStringProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,38 @@

namespace Hedii\LaravelGelfLogger\Tests\Processors;

use Carbon\Carbon;
use DateTimeImmutable;
use Hedii\LaravelGelfLogger\Processors\NullStringProcessor;
use Monolog\Level;
use Monolog\LogRecord;
use PHPUnit\Framework\TestCase;

class NullStringProcessorTest extends TestCase
{
/** @test */
public function it_should_transform_null_string_to_null(): void
{
$payload = [
'context' => [
$payload = new LogRecord(
datetime: new DateTimeImmutable(),
channel: 'gelf',
level: Level::Debug,
message: 'message',
context: [
'key1' => 'bar',
'key2' => 'NULL',
'key3' => 'null',
'key4' => null,
],
];
]
);

$processor = new NullStringProcessor();

$this->assertSame([
'context' => [
'key1' => 'bar',
'key2' => null,
'key3' => null,
'key4' => null,
],
], $processor($payload));
'key1' => 'bar',
'key2' => null,
'key3' => null,
'key4' => null,
], $processor($payload)->context);
}
}
44 changes: 38 additions & 6 deletions tests/Processors/RenameIdFieldProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,52 @@

namespace Hedii\LaravelGelfLogger\Tests\Processors;

use DateTimeImmutable;
use Hedii\LaravelGelfLogger\Processors\RenameIdFieldProcessor;
use Monolog\Level;
use Monolog\LogRecord;
use PHPUnit\Framework\TestCase;

class RenameIdFieldProcessorTest extends TestCase
{
/** @test */
public function it_should_rename_id_field(): void
/**
* @test
* @dataProvider dataProvider
*/
public function it_should_rename_id_field(array $payloadContext, array $expected): void
{
$payload = [
'context' => ['id' => 'bar'],
];
$payload = new LogRecord(
datetime: new DateTimeImmutable(),
channel: 'gelf',
level: Level::Debug,
message: 'message',
context: $payloadContext
);

$processor = new RenameIdFieldProcessor();

$this->assertSame(['context' => ['_id' => 'bar']], $processor($payload));
$this->assertSame($expected, $processor($payload)->context);
}

public static function dataProvider(): array
{
return [
'having neither underscore id nor id' => [
['someotherfield' => 'someothervalue'],
['someotherfield' => 'someothervalue'],
],
'having id and underscore id' => [
['id' => 'bar', '_id' => 'bar2'],
['_id' => 'bar'],
],
'having id and not underscore id' => [
['id' => 'bar'],
['_id' => 'bar'],
],
'having no id and underscore id' => [
['_id' => 'bar', 'field1' => 'value1'],
['_id' => 'bar', 'field1' => 'value1'],
],
];
}
}

0 comments on commit 47d82c3

Please sign in to comment.