1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-12 16:14:08 +02:00

Merge remote-tracking branch 'idio/master'

This commit is contained in:
Jordi Boggiano
2013-09-16 11:53:31 +02:00
2 changed files with 195 additions and 6 deletions

View File

@@ -21,6 +21,10 @@ namespace Monolog\Formatter;
*/ */
class LogstashFormatter extends NormalizerFormatter class LogstashFormatter extends NormalizerFormatter
{ {
const V0 = 0;
const V1 = 1;
/** /**
* @var string the name of the system for the Logstash log message, used to fill the @source field * @var string the name of the system for the Logstash log message, used to fill the @source field
*/ */
@@ -41,13 +45,18 @@ class LogstashFormatter extends NormalizerFormatter
*/ */
protected $contextPrefix; protected $contextPrefix;
/**
* @var integer LogStash format version to use
*/
protected $version;
/** /**
* @param string $applicationName the application that sends the data, used as the "type" field of logstash * @param string $applicationName the application that sends the data, used as the "type" field of logstash
* @param string $systemName the system/machine name, used as the "source" field of logstash, defaults to the hostname of the machine * @param string $systemName the system/machine name, used as the "source" field of logstash, defaults to the hostname of the machine
* @param string $extraPrefix prefix for extra keys inside logstash "fields" * @param string $extraPrefix prefix for extra keys inside logstash "fields"
* @param string $contextPrefix prefix for context keys inside logstash "fields", defaults to ctxt_ * @param string $contextPrefix prefix for context keys inside logstash "fields", defaults to ctxt_
*/ */
public function __construct($applicationName, $systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_') public function __construct($applicationName, $systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_', $version = self::V0)
{ {
//log stash requires a ISO 8601 format date //log stash requires a ISO 8601 format date
parent::__construct('c'); parent::__construct('c');
@@ -57,6 +66,8 @@ class LogstashFormatter extends NormalizerFormatter
$this->extraPrefix = $extraPrefix; $this->extraPrefix = $extraPrefix;
$this->contextPrefix = $contextPrefix; $this->contextPrefix = $contextPrefix;
$this->version = $version;
} }
/** /**
@@ -65,19 +76,36 @@ class LogstashFormatter extends NormalizerFormatter
public function format(array $record) public function format(array $record)
{ {
$record = parent::format($record); $record = parent::format($record);
switch ($this->version) {
case self::V1:
$message = $this->formatV1($record);
break;
case self::V0:
default:
$message = $this->formatV0($record);
break;
}
return $this->toJson($message) . "\n";
}
protected function formatV0(array $record)
{
$message = array( $message = array(
'@timestamp' => $record['datetime'], '@timestamp' => $record['datetime'],
'@message' => $record['message'], '@message' => $record['message'],
'@tags' => array($record['channel']), '@tags' => array($record['channel']),
'@source' => $this->systemName '@source' => $this->systemName,
'@fields' => array(
'channel' => $record['channel'],
'level' => $record['level']
)
); );
if ($this->applicationName) { if ($this->applicationName) {
$message['@type'] = $this->applicationName; $message['@type'] = $this->applicationName;
} }
$message['@fields'] = array();
$message['@fields']['channel'] = $record['channel'];
$message['@fields']['level'] = $record['level'];
if (isset($record['extra']['server'])) { if (isset($record['extra']['server'])) {
$message['@source_host'] = $record['extra']['server']; $message['@source_host'] = $record['extra']['server'];
@@ -93,6 +121,33 @@ class LogstashFormatter extends NormalizerFormatter
$message['@fields'][$this->contextPrefix . $key] = $val; $message['@fields'][$this->contextPrefix . $key] = $val;
} }
return json_encode($message) . "\n"; return $message;
}
protected function formatV1(array $record)
{
$message = array(
'@timestamp' => $record['datetime'],
'@version' => 1,
'message' => $record['message'],
'host' => $this->systemName,
'type' => $record['channel'],
'channel' => $record['channel'],
'level' => $record['level_name']
);
if ($this->applicationName) {
$message['type'] = $this->applicationName;
}
foreach ($record['extra'] as $key => $val) {
$message[$this->extraPrefix . $key] = $val;
}
foreach ($record['context'] as $key => $val) {
$message[$this->contextPrefix . $key] = $val;
}
return $message;
} }
} }

View File

@@ -157,4 +157,138 @@ class LogstashFormatterTest extends \PHPUnit_Framework_TestCase
$this->assertArrayHasKey('@type', $message); $this->assertArrayHasKey('@type', $message);
$this->assertEquals('app', $message['@type']); $this->assertEquals('app', $message['@type']);
} }
/**
* @covers Monolog\Formatter\LogstashFormatter::format
*/
public function testDefaultFormatterV1()
{
$formatter = new LogstashFormatter('test', 'hostname', null, 'ctxt_', LogstashFormatter::V1);
$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+00:00", $message['@timestamp']);
$this->assertEquals("1", $message['@version']);
$this->assertEquals('log', $message['message']);
$this->assertEquals('meh', $message['channel']);
$this->assertEquals('ERROR', $message['level']);
$this->assertEquals('test', $message['type']);
$this->assertEquals('hostname', $message['host']);
$formatter = new LogstashFormatter('mysystem', null, null, 'ctxt_', LogstashFormatter::V1);
$message = json_decode($formatter->format($record), true);
$this->assertEquals('mysystem', $message['type']);
}
/**
* @covers Monolog\Formatter\LogstashFormatter::format
*/
public function testFormatWithFileAndLineV1()
{
$formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1);
$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['file']);
$this->assertEquals(14, $message['line']);
}
/**
* @covers Monolog\Formatter\LogstashFormatter::format
*/
public function testFormatWithContextV1()
{
$formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1);
$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('ctxt_from', $message);
$this->assertEquals('logger', $message['ctxt_from']);
// Test with extraPrefix
$formatter = new LogstashFormatter('test', null, null, 'CTX', LogstashFormatter::V1);
$message = json_decode($formatter->format($record), true);
$this->assertArrayHasKey('CTXfrom', $message);
$this->assertEquals('logger', $message['CTXfrom']);
}
/**
* @covers Monolog\Formatter\LogstashFormatter::format
*/
public function testFormatWithExtraV1()
{
$formatter = new LogstashFormatter('test', null, null, 'ctxt_', LogstashFormatter::V1);
$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('key', $message);
$this->assertEquals('pair', $message['key']);
// Test with extraPrefix
$formatter = new LogstashFormatter('test', null, 'EXT', 'ctxt_', LogstashFormatter::V1);
$message = json_decode($formatter->format($record), true);
$this->assertArrayHasKey('EXTkey', $message);
$this->assertEquals('pair', $message['EXTkey']);
}
public function testFormatWithApplicationNameV1()
{
$formatter = new LogstashFormatter('app', 'test', null, 'ctxt_', LogstashFormatter::V1);
$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']);
}
} }