Skip to content

Commit 11d702b

Browse files
dezonepaul_sleepy
andauthored
done (#16)
Co-authored-by: paul_sleepy <pavel.chi@okwork.io>
1 parent 680bca0 commit 11d702b

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Umbrellio\TableSync\Monolog\Formatter;
6+
7+
use Monolog\Utils;
8+
9+
class JsonTableSyncFormatter extends TableSyncFormatter
10+
{
11+
public function format(array $record)
12+
{
13+
$record = parent::format($record);
14+
$formatted = [
15+
'datetime' => $record['datetime'],
16+
'message' => $record['message'],
17+
'direction' => $record['direction'],
18+
'routing' => $record['routing'],
19+
'model' => $record['model'],
20+
'event' => $record['event'],
21+
'count' => count($record['attributes']),
22+
];
23+
24+
return Utils::jsonEncode($formatted, Utils::DEFAULT_JSON_FLAGS, true) . "\n";
25+
}
26+
}

tests/unit/Logging/FormatterTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use InfluxDB\Point;
88
use Umbrellio\TableSync\Messages\PublishMessage;
99
use Umbrellio\TableSync\Monolog\Formatter\InfluxDBFormatter;
10+
use Umbrellio\TableSync\Monolog\Formatter\JsonTableSyncFormatter;
1011
use Umbrellio\TableSync\Monolog\Formatter\LineTableSyncFormatter;
1112
use Umbrellio\TableSync\Monolog\Formatter\TableSyncFormatter;
1213
use Umbrellio\TableSync\Rabbit\Config\PublishMessage as Config;
@@ -78,6 +79,18 @@ public function influxDbFormatter(): void
7879
$this->assertSame(['model', 'event', 'direction'], array_keys($format[0]->getTags()));
7980
}
8081

82+
/**
83+
* @test
84+
*/
85+
public function jsonTableSyncFormat(): void
86+
{
87+
$jsonTableSyncFormatter = new JsonTableSyncFormatter();
88+
$format = $jsonTableSyncFormatter->format($this->getDummyRecord());
89+
$expected = '{"datetime":"datetime","message":"message","direction":"direction","routing":"routing_key","model":"model","event":"update","count":1}' . "\n";
90+
$this->assertIsString($format);
91+
$this->assertSame($expected, $format);
92+
}
93+
8194
private function getDummyRecord(): array
8295
{
8396
$message = new PublishMessage('model', 'event', 'routingKey', [

0 commit comments

Comments
 (0)