diff --git a/src/Monolog/Formatter/LogstashFormatter.php b/src/Monolog/Formatter/LogstashFormatter.php index 7aa8ad33..f8839595 100644 --- a/src/Monolog/Formatter/LogstashFormatter.php +++ b/src/Monolog/Formatter/LogstashFormatter.php @@ -21,6 +21,10 @@ namespace Monolog\Formatter; */ 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 */ @@ -41,13 +45,18 @@ class LogstashFormatter extends NormalizerFormatter */ 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 $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 $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 parent::__construct('c'); @@ -57,6 +66,8 @@ class LogstashFormatter extends NormalizerFormatter $this->extraPrefix = $extraPrefix; $this->contextPrefix = $contextPrefix; + + $this->version = $version; } /** @@ -65,19 +76,36 @@ class LogstashFormatter extends NormalizerFormatter public function format(array $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( '@timestamp' => $record['datetime'], '@message' => $record['message'], '@tags' => array($record['channel']), - '@source' => $this->systemName + '@source' => $this->systemName, + '@fields' => array( + 'channel' => $record['channel'], + 'level' => $record['level'] + ) ); if ($this->applicationName) { $message['@type'] = $this->applicationName; } - $message['@fields'] = array(); - $message['@fields']['channel'] = $record['channel']; - $message['@fields']['level'] = $record['level']; if (isset($record['extra']['server'])) { $message['@source_host'] = $record['extra']['server']; @@ -93,6 +121,33 @@ class LogstashFormatter extends NormalizerFormatter $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; } } diff --git a/src/Monolog/Formatter/LogstashV1Formatter.php b/src/Monolog/Formatter/LogstashV1Formatter.php deleted file mode 100644 index aa09462b..00000000 --- a/src/Monolog/Formatter/LogstashV1Formatter.php +++ /dev/null @@ -1,92 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Monolog\Formatter; - -/** - * Serializes a log message to Logstash Event Format - * - * @see http://logstash.net/ - * @see https://github.com/logstash/logstash/blob/master/lib/logstash/event.rb - * - * @author Tim Mower - */ -class LogstashV1Formatter extends NormalizerFormatter -{ - /** - * @var string the name of the system for the Logstash log message, used to fill the @source field - */ - protected $systemName; - - /** - * @var string an application name for the Logstash log message, used to fill the @type field - */ - protected $applicationName; - - /** - * @var string a prefix for 'extra' fields from the Monolog record (optional) - */ - protected $extraPrefix; - - /** - * @var string a prefix for 'context' fields from the Monolog record (optional) - */ - protected $contextPrefix; - - /** - * @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 $extraPrefix prefix for extra keys inside logstash "fields" - * @param string $contextPrefix prefix for context keys inside logstash "fields", defaults to ctxt_ - */ - public function __construct($applicationName, $systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_') - { - //log stash requires a ISO 8601 format date - parent::__construct('c'); - - $this->systemName = $systemName ?: gethostname(); - $this->applicationName = $applicationName; - - $this->extraPrefix = $extraPrefix; - $this->contextPrefix = $contextPrefix; - } - - /** - * {@inheritdoc} - */ - public function format(array $record) - { - $record = parent::format($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 $this->toJson($message) . "\n"; - } -} diff --git a/tests/Monolog/Formatter/LogstashFormatterTest.php b/tests/Monolog/Formatter/LogstashFormatterTest.php index 6b012dea..4fb5b3b2 100644 --- a/tests/Monolog/Formatter/LogstashFormatterTest.php +++ b/tests/Monolog/Formatter/LogstashFormatterTest.php @@ -157,4 +157,138 @@ class LogstashFormatterTest extends \PHPUnit_Framework_TestCase $this->assertArrayHasKey('@type', $message); $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']); + } } diff --git a/tests/Monolog/Formatter/LogstashV1FormatterTest.php b/tests/Monolog/Formatter/LogstashV1FormatterTest.php deleted file mode 100644 index be055f51..00000000 --- a/tests/Monolog/Formatter/LogstashV1FormatterTest.php +++ /dev/null @@ -1,152 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Monolog\Formatter; - -use Monolog\Logger; -use Monolog\Formatter\LogstashFormatter; - -class LogstashV1FormatterTest extends \PHPUnit_Framework_TestCase -{ - - /** - * @covers Monolog\Formatter\LogstashFormatter::format - */ - public function testDefaultFormatter() - { - $formatter = new LogstashV1Formatter('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+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 LogstashV1Formatter('mysystem'); - - $message = json_decode($formatter->format($record), true); - - $this->assertEquals('mysystem', $message['type']); - } - - /** - * @covers Monolog\Formatter\LogstashFormatter::format - */ - public function testFormatWithFileAndLine() - { - $formatter = new LogstashV1Formatter('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['file']); - $this->assertEquals(14, $message['line']); - } - - /** - * @covers Monolog\Formatter\LogstashFormatter::format - */ - public function testFormatWithContext() - { - $formatter = new LogstashV1Formatter('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('ctxt_from', $message); - $this->assertEquals('logger', $message['ctxt_from']); - - // Test with extraPrefix - $formatter = new LogstashV1Formatter('test', null, null, 'CTX'); - $message = json_decode($formatter->format($record), true); - - $this->assertArrayHasKey('CTXfrom', $message); - $this->assertEquals('logger', $message['CTXfrom']); - - } - - /** - * @covers Monolog\Formatter\LogstashFormatter::format - */ - public function testFormatWithExtra() - { - $formatter = new LogstashV1Formatter('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('key', $message); - $this->assertEquals('pair', $message['key']); - - // Test with extraPrefix - $formatter = new LogstashV1Formatter('test', null, 'EXT'); - $message = json_decode($formatter->format($record), true); - - $this->assertArrayHasKey('EXTkey', $message); - $this->assertEquals('pair', $message['EXTkey']); - } - - public function testFormatWithApplicationName() - { - $formatter = new LogstashV1Formatter('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']); - } -}