1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-07 13:46:38 +02:00

Fix logstash formatter tests

This commit is contained in:
Jordi Boggiano
2016-05-20 20:07:04 +01:00
parent 1fb211e43f
commit 1161b23148

View File

@@ -22,152 +22,12 @@ class LogstashFormatterTest extends \PHPUnit_Framework_TestCase
return parent::tearDown(); return parent::tearDown();
} }
/**
* @covers Monolog\Formatter\LogstashFormatter::format
*/
public function testDefaultFormatter()
{
$formatter = new LogstashFormatter('test', 'hostname');
$record = array(
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => array(),
'datetime' => new \DateTime("@0"),
'extra' => array(),
'message' => 'log',
);
$message = json_decode($formatter->format($record), true);
$this->assertEquals("1970-01-01T00:00:00.000000+00:00", $message['@timestamp']);
$this->assertEquals('log', $message['@message']);
$this->assertEquals('meh', $message['@fields']['channel']);
$this->assertContains('meh', $message['@tags']);
$this->assertEquals(Logger::ERROR, $message['@fields']['level']);
$this->assertEquals('test', $message['@type']);
$this->assertEquals('hostname', $message['@source']);
$formatter = new LogstashFormatter('mysystem');
$message = json_decode($formatter->format($record), true);
$this->assertEquals('mysystem', $message['@type']);
}
/**
* @covers Monolog\Formatter\LogstashFormatter::format
*/
public function testFormatWithFileAndLine()
{
$formatter = new LogstashFormatter('test');
$record = array(
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => array('from' => 'logger'),
'datetime' => new \DateTime("@0"),
'extra' => array('file' => 'test', 'line' => 14),
'message' => 'log',
);
$message = json_decode($formatter->format($record), true);
$this->assertEquals('test', $message['@fields']['file']);
$this->assertEquals(14, $message['@fields']['line']);
}
/**
* @covers Monolog\Formatter\LogstashFormatter::format
*/
public function testFormatWithContext()
{
$formatter = new LogstashFormatter('test');
$record = array(
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => array('from' => 'logger'),
'datetime' => new \DateTime("@0"),
'extra' => array('key' => 'pair'),
'message' => 'log',
);
$message = json_decode($formatter->format($record), true);
$message_array = $message['@fields'];
$this->assertArrayHasKey('ctxt_from', $message_array);
$this->assertEquals('logger', $message_array['ctxt_from']);
// Test with extraPrefix
$formatter = new LogstashFormatter('test', null, null, 'CTX');
$message = json_decode($formatter->format($record), true);
$message_array = $message['@fields'];
$this->assertArrayHasKey('CTXfrom', $message_array);
$this->assertEquals('logger', $message_array['CTXfrom']);
}
/**
* @covers Monolog\Formatter\LogstashFormatter::format
*/
public function testFormatWithExtra()
{
$formatter = new LogstashFormatter('test');
$record = array(
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => array('from' => 'logger'),
'datetime' => new \DateTime("@0"),
'extra' => array('key' => 'pair'),
'message' => 'log',
);
$message = json_decode($formatter->format($record), true);
$message_array = $message['@fields'];
$this->assertArrayHasKey('key', $message_array);
$this->assertEquals('pair', $message_array['key']);
// Test with extraPrefix
$formatter = new LogstashFormatter('test', null, 'EXT');
$message = json_decode($formatter->format($record), true);
$message_array = $message['@fields'];
$this->assertArrayHasKey('EXTkey', $message_array);
$this->assertEquals('pair', $message_array['EXTkey']);
}
public function testFormatWithApplicationName()
{
$formatter = new LogstashFormatter('app', 'test');
$record = array(
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => array('from' => 'logger'),
'datetime' => new \DateTime("@0"),
'extra' => array('key' => 'pair'),
'message' => 'log',
);
$message = json_decode($formatter->format($record), true);
$this->assertArrayHasKey('@type', $message);
$this->assertEquals('app', $message['@type']);
}
/** /**
* @covers Monolog\Formatter\LogstashFormatter::format * @covers Monolog\Formatter\LogstashFormatter::format
*/ */
public function testDefaultFormatterV1() public function testDefaultFormatterV1()
{ {
$formatter = new LogstashFormatter('test', 'hostname', null, 'ctxt_', LogstashFormatter::V1); $formatter = new LogstashFormatter('test', 'hostname', null, 'ctxt_');
$record = array( $record = array(
'level' => Logger::ERROR, 'level' => Logger::ERROR,
'level_name' => 'ERROR', 'level_name' => 'ERROR',
@@ -188,7 +48,7 @@ class LogstashFormatterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('test', $message['type']); $this->assertEquals('test', $message['type']);
$this->assertEquals('hostname', $message['host']); $this->assertEquals('hostname', $message['host']);
$formatter = new LogstashFormatter('mysystem', null, null, 'ctxt_', LogstashFormatter::V1); $formatter = new LogstashFormatter('mysystem', null, null, 'ctxt_');
$message = json_decode($formatter->format($record), true); $message = json_decode($formatter->format($record), true);
@@ -200,7 +60,7 @@ class LogstashFormatterTest extends \PHPUnit_Framework_TestCase
*/ */
public function testFormatWithFileAndLineV1() public function testFormatWithFileAndLineV1()
{ {
$formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1); $formatter = new LogstashFormatter('test', null, null, 'ctxt_');
$record = array( $record = array(
'level' => Logger::ERROR, 'level' => Logger::ERROR,
'level_name' => 'ERROR', 'level_name' => 'ERROR',
@@ -222,7 +82,7 @@ class LogstashFormatterTest extends \PHPUnit_Framework_TestCase
*/ */
public function testFormatWithContextV1() public function testFormatWithContextV1()
{ {
$formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1); $formatter = new LogstashFormatter('test', null, null, 'ctxt_');
$record = array( $record = array(
'level' => Logger::ERROR, 'level' => Logger::ERROR,
'level_name' => 'ERROR', 'level_name' => 'ERROR',
@@ -239,7 +99,7 @@ class LogstashFormatterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('logger', $message['ctxt_from']); $this->assertEquals('logger', $message['ctxt_from']);
// Test with extraPrefix // Test with extraPrefix
$formatter = new LogstashFormatter('test', null, null, 'CTX', LogstashFormatter::V1); $formatter = new LogstashFormatter('test', null, null, 'CTX');
$message = json_decode($formatter->format($record), true); $message = json_decode($formatter->format($record), true);
$this->assertArrayHasKey('CTXfrom', $message); $this->assertArrayHasKey('CTXfrom', $message);
@@ -251,7 +111,7 @@ class LogstashFormatterTest extends \PHPUnit_Framework_TestCase
*/ */
public function testFormatWithExtraV1() public function testFormatWithExtraV1()
{ {
$formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1); $formatter = new LogstashFormatter('test', null, null, 'ctxt_');
$record = array( $record = array(
'level' => Logger::ERROR, 'level' => Logger::ERROR,
'level_name' => 'ERROR', 'level_name' => 'ERROR',
@@ -268,7 +128,7 @@ class LogstashFormatterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('pair', $message['key']); $this->assertEquals('pair', $message['key']);
// Test with extraPrefix // Test with extraPrefix
$formatter = new LogstashFormatter('test', null, 'EXT', 'ctxt_', LogstashFormatter::V1); $formatter = new LogstashFormatter('test', null, 'EXT', 'ctxt_');
$message = json_decode($formatter->format($record), true); $message = json_decode($formatter->format($record), true);
$this->assertArrayHasKey('EXTkey', $message); $this->assertArrayHasKey('EXTkey', $message);
@@ -277,7 +137,7 @@ class LogstashFormatterTest extends \PHPUnit_Framework_TestCase
public function testFormatWithApplicationNameV1() public function testFormatWithApplicationNameV1()
{ {
$formatter = new LogstashFormatter('app', 'test', null, 'ctxt_', LogstashFormatter::V1); $formatter = new LogstashFormatter('app', 'test', null, 'ctxt_');
$record = array( $record = array(
'level' => Logger::ERROR, 'level' => Logger::ERROR,
'level_name' => 'ERROR', 'level_name' => 'ERROR',
@@ -312,12 +172,11 @@ class LogstashFormatterTest extends \PHPUnit_Framework_TestCase
$message = json_decode($formatter->format($record), true); $message = json_decode($formatter->format($record), true);
$this->assertEquals("1970-01-01T00:00:00.000000+00:00", $message['@timestamp']); $this->assertEquals("1970-01-01T00:00:00.000000+00:00", $message['@timestamp']);
$this->assertEquals('log', $message['@message']); $this->assertEquals('log', $message['message']);
$this->assertEquals('¯\_(ツ)_/¯', $message['@fields']['channel']); $this->assertEquals('¯\_(ツ)_/¯', $message['channel']);
$this->assertContains('¯\_(ツ)_/¯', $message['@tags']); $this->assertEquals('ERROR', $message['level']);
$this->assertEquals(Logger::ERROR, $message['@fields']['level']); $this->assertEquals('test', $message['type']);
$this->assertEquals('test', $message['@type']); $this->assertEquals('hostname', $message['host']);
$this->assertEquals('hostname', $message['@source']); $this->assertEquals('ÖWN; FBCR/OrangeEspaña; Versão/4.0; Färist', $message['user_agent']);
$this->assertEquals('ÖWN; FBCR/OrangeEspaña; Versão/4.0; Färist', $message['@fields']['user_agent']);
} }
} }