Skip to content

Commit 47d82c3

Browse files
authored
Merge pull request #42 from patriziotomato/upgrade-processors
Upgrade processors to Monolog 3 and fixe issue in RenameIdFieldProcessor
2 parents 2a4f42d + 986d644 commit 47d82c3

File tree

4 files changed

+71
-28
lines changed

4 files changed

+71
-28
lines changed

src/Processors/NullStringProcessor.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22

33
namespace Hedii\LaravelGelfLogger\Processors;
44

5+
use Monolog\LogRecord;
6+
57
class NullStringProcessor
68
{
79
/**
810
* Transform a "NULL" string record into a null value.
911
*/
10-
public function __invoke(array $record): array
12+
public function __invoke(LogRecord $record): LogRecord
1113
{
12-
foreach ($record['context'] as $key => $value) {
14+
$context = $record->context;
15+
16+
foreach ($context as $key => $value) {
1317
if (is_string($value) && strtoupper($value) === 'NULL') {
14-
$record['context'][$key] = null;
18+
$context[$key] = null;
1519
}
1620
}
1721

18-
return $record;
22+
return $record->with(context: $context);
1923
}
2024
}

src/Processors/RenameIdFieldProcessor.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22

33
namespace Hedii\LaravelGelfLogger\Processors;
44

5+
use Monolog\LogRecord;
6+
57
class RenameIdFieldProcessor
68
{
79
/**
810
* Rename "id" field to "_id" (additional field 'id' is not allowed).
911
*
1012
* @see https://github.com/hedii/laravel-gelf-logger/issues/33
1113
*/
12-
public function __invoke(array $record): array
14+
public function __invoke(LogRecord $record): LogRecord
1315
{
14-
foreach ($record['context'] as $key => $value) {
15-
if ($key === 'id' && ! array_key_exists('_id', $record['context'])) {
16-
unset($record['context']['id']);
16+
$context = $record->context;
1717

18-
$record['context']['_id'] = $value;
19-
}
18+
if (array_key_exists('id', $context)) {
19+
$context['_id'] = $context['id'];
20+
unset($context['id']);
2021
}
2122

22-
return $record;
23+
return $record->with(context: $context);
2324
}
2425
}

tests/Processors/NullStringProcessorTest.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,38 @@
22

33
namespace Hedii\LaravelGelfLogger\Tests\Processors;
44

5+
use Carbon\Carbon;
6+
use DateTimeImmutable;
57
use Hedii\LaravelGelfLogger\Processors\NullStringProcessor;
8+
use Monolog\Level;
9+
use Monolog\LogRecord;
610
use PHPUnit\Framework\TestCase;
711

812
class NullStringProcessorTest extends TestCase
913
{
1014
/** @test */
1115
public function it_should_transform_null_string_to_null(): void
1216
{
13-
$payload = [
14-
'context' => [
17+
$payload = new LogRecord(
18+
datetime: new DateTimeImmutable(),
19+
channel: 'gelf',
20+
level: Level::Debug,
21+
message: 'message',
22+
context: [
1523
'key1' => 'bar',
1624
'key2' => 'NULL',
1725
'key3' => 'null',
1826
'key4' => null,
19-
],
20-
];
27+
]
28+
);
2129

2230
$processor = new NullStringProcessor();
2331

2432
$this->assertSame([
25-
'context' => [
26-
'key1' => 'bar',
27-
'key2' => null,
28-
'key3' => null,
29-
'key4' => null,
30-
],
31-
], $processor($payload));
33+
'key1' => 'bar',
34+
'key2' => null,
35+
'key3' => null,
36+
'key4' => null,
37+
], $processor($payload)->context);
3238
}
3339
}

tests/Processors/RenameIdFieldProcessorTest.php

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,52 @@
22

33
namespace Hedii\LaravelGelfLogger\Tests\Processors;
44

5+
use DateTimeImmutable;
56
use Hedii\LaravelGelfLogger\Processors\RenameIdFieldProcessor;
7+
use Monolog\Level;
8+
use Monolog\LogRecord;
69
use PHPUnit\Framework\TestCase;
710

811
class RenameIdFieldProcessorTest extends TestCase
912
{
10-
/** @test */
11-
public function it_should_rename_id_field(): void
13+
/**
14+
* @test
15+
* @dataProvider dataProvider
16+
*/
17+
public function it_should_rename_id_field(array $payloadContext, array $expected): void
1218
{
13-
$payload = [
14-
'context' => ['id' => 'bar'],
15-
];
19+
$payload = new LogRecord(
20+
datetime: new DateTimeImmutable(),
21+
channel: 'gelf',
22+
level: Level::Debug,
23+
message: 'message',
24+
context: $payloadContext
25+
);
1626

1727
$processor = new RenameIdFieldProcessor();
1828

19-
$this->assertSame(['context' => ['_id' => 'bar']], $processor($payload));
29+
$this->assertSame($expected, $processor($payload)->context);
30+
}
31+
32+
public static function dataProvider(): array
33+
{
34+
return [
35+
'having neither underscore id nor id' => [
36+
['someotherfield' => 'someothervalue'],
37+
['someotherfield' => 'someothervalue'],
38+
],
39+
'having id and underscore id' => [
40+
['id' => 'bar', '_id' => 'bar2'],
41+
['_id' => 'bar'],
42+
],
43+
'having id and not underscore id' => [
44+
['id' => 'bar'],
45+
['_id' => 'bar'],
46+
],
47+
'having no id and underscore id' => [
48+
['_id' => 'bar', 'field1' => 'value1'],
49+
['_id' => 'bar', 'field1' => 'value1'],
50+
],
51+
];
2052
}
2153
}

0 commit comments

Comments
 (0)