Page MenuHomeWickedGov Phorge

LogstashFormatterTest.php
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

LogstashFormatterTest.php

<?php
namespace MediaWiki\Tests\Logger\Monolog;
use MediaWiki\Logger\Monolog\LogstashFormatter;
/**
* @covers \MediaWiki\Logger\Monolog\LogstashFormatter
*/
class LogstashFormatterTest extends \MediaWikiUnitTestCase {
/**
* @dataProvider provideV1
* @param array $record The input record.
* @param array $expected Associative array of expected keys and their values.
* @param array $notExpected List of keys that should not exist.
*/
public function testV1( array $record, array $expected, array $notExpected ) {
$formatter = new LogstashFormatter( 'app', 'system', '', '', LogstashFormatter::V1 );
$formatted = json_decode( $formatter->format( $record ), true );
foreach ( $expected as $key => $value ) {
$this->assertArrayHasKey( $key, $formatted );
$this->assertSame( $value, $formatted[$key] );
}
foreach ( $notExpected as $key ) {
$this->assertArrayNotHasKey( $key, $formatted );
}
}
public static function provideV1() {
return [
[
[ 'extra' => [ 'foo' => 1 ], 'context' => [ 'bar' => 2 ] ],
[ 'foo' => 1, 'bar' => 2 ],
[ 'logstash_formatter_key_conflict' ],
],
[
[ 'extra' => [ 'url' => 1 ], 'context' => [ 'url' => 2 ] ],
[ 'url' => 1, 'c_url' => 2, 'logstash_formatter_key_conflict' => [ 'url' ] ],
[],
],
[
[ 'channel' => 'x', 'context' => [ 'channel' => 'y' ] ],
[ 'channel' => 'x', 'c_channel' => 'y',
'logstash_formatter_key_conflict' => [ 'channel' ] ],
[],
],
];
}
public function testV1WithPrefix() {
$formatter = new LogstashFormatter( 'app', 'system', '', 'ctx_', LogstashFormatter::V1 );
$record = [ 'extra' => [ 'url' => 1 ], 'context' => [ 'url' => 2 ] ];
$formatted = json_decode( $formatter->format( $record ), true );
$this->assertArrayHasKey( 'url', $formatted );
$this->assertSame( 1, $formatted['url'] );
$this->assertArrayHasKey( 'ctx_url', $formatted );
$this->assertSame( 2, $formatted['ctx_url'] );
$this->assertArrayNotHasKey( 'c_url', $formatted );
}
}

File Metadata

Mime Type
text/x-php
Expires
Sun, Nov 22, 11:40 (3 h, 27 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
5f/39/871ceef8f4d419bd6fec7d4f46d0
Default Alt Text
LogstashFormatterTest.php (1 KB)

Event Timeline