3 namespace MediaWiki\Logger\Monolog
;
5 class LogstashFormatterTest
extends \PHPUnit\Framework\TestCase
{
7 * @dataProvider provideV1
8 * @covers MediaWiki\Logger\Monolog\LogstashFormatter::formatV1
9 * @param array $record The input record.
10 * @param array $expected Associative array of expected keys and their values.
11 * @param array $notExpected List of keys that should not exist.
13 public function testV1( array $record, array $expected, array $notExpected ) {
14 $formatter = new LogstashFormatter( 'app', 'system', null, null, LogstashFormatter
::V1
);
15 $formatted = json_decode( $formatter->format( $record ), true );
16 foreach ( $expected as $key => $value ) {
17 $this->assertArrayHasKey( $key, $formatted );
18 $this->assertSame( $value, $formatted[$key] );
20 foreach ( $notExpected as $key ) {
21 $this->assertArrayNotHasKey( $key, $formatted );
25 public function provideV1() {
28 [ 'extra' => [ 'foo' => 1 ], 'context' => [ 'bar' => 2 ] ],
29 [ 'foo' => 1, 'bar' => 2 ],
30 [ 'logstash_formatter_key_conflict' ],
33 [ 'extra' => [ 'url' => 1 ], 'context' => [ 'url' => 2 ] ],
34 [ 'url' => 1, 'c_url' => 2, 'logstash_formatter_key_conflict' => [ 'url' ] ],
38 [ 'channel' => 'x', 'context' => [ 'channel' => 'y' ] ],
39 [ 'channel' => 'x', 'c_channel' => 'y',
40 'logstash_formatter_key_conflict' => [ 'channel' ] ],
47 * @covers MediaWiki\Logger\Monolog\LogstashFormatter::formatV1
49 public function testV1WithPrefix() {
50 $formatter = new LogstashFormatter( 'app', 'system', null, 'ctx_', LogstashFormatter
::V1
);
51 $record = [ 'extra' => [ 'url' => 1 ], 'context' => [ 'url' => 2 ] ];
52 $formatted = json_decode( $formatter->format( $record ), true );
53 $this->assertArrayHasKey( 'url', $formatted );
54 $this->assertSame( 1, $formatted['url'] );
55 $this->assertArrayHasKey( 'ctx_url', $formatted );
56 $this->assertSame( 2, $formatted['ctx_url'] );
57 $this->assertArrayNotHasKey( 'c_url', $formatted );