From 798a039040a32c931fb8e7934d90a2637a6353ab Mon Sep 17 00:00:00 2001 From: Tim Mower Date: Mon, 10 Dec 2012 12:35:34 +0000 Subject: [PATCH] Add applicationName option which gets set to the logstash @type field --- src/Monolog/Formatter/LogstashFormatter.php | 13 +++++++++-- .../Formatter/LogstashFormatterTest.php | 23 +++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/Monolog/Formatter/LogstashFormatter.php b/src/Monolog/Formatter/LogstashFormatter.php index 644dee64..d9772033 100644 --- a/src/Monolog/Formatter/LogstashFormatter.php +++ b/src/Monolog/Formatter/LogstashFormatter.php @@ -23,10 +23,15 @@ use Monolog\Logger; class LogstashFormatter extends NormalizerFormatter { /** - * @var string the name of the system for the Gelf log message + * @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) */ @@ -38,12 +43,13 @@ class LogstashFormatter extends NormalizerFormatter protected $contextPrefix; - public function __construct($systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_') + public function __construct($systemName = null, $applicationName = 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; @@ -62,6 +68,9 @@ class LogstashFormatter extends NormalizerFormatter '@source' => $this->systemName ); + if (isset($this->applicationName)) { + $message['@type'] = $this->applicationName; + } $message['@fields'] = array(); $message['@fields']['channel'] = $record['channel']; $message['@fields']['level'] = $record['level']; diff --git a/tests/Monolog/Formatter/LogstashFormatterTest.php b/tests/Monolog/Formatter/LogstashFormatterTest.php index 9329fd59..f94df6f6 100644 --- a/tests/Monolog/Formatter/LogstashFormatterTest.php +++ b/tests/Monolog/Formatter/LogstashFormatterTest.php @@ -96,7 +96,7 @@ class LogstashFormatterTest extends \PHPUnit_Framework_TestCase $this->assertEquals('logger', $message_array['ctxt_from']); // Test with extraPrefix - $formatter = new LogstashFormatter('test', null, 'CTX'); + $formatter = new LogstashFormatter('test', null, null, 'CTX'); $message = json_decode($formatter->format($record), true); @@ -131,7 +131,7 @@ class LogstashFormatterTest extends \PHPUnit_Framework_TestCase $this->assertEquals('pair', $message_array['key']); // Test with extraPrefix - $formatter = new LogstashFormatter('test', 'EXT'); + $formatter = new LogstashFormatter('test', null, 'EXT'); $message = json_decode($formatter->format($record), true); $message_array = $message['@fields']; @@ -139,4 +139,23 @@ class LogstashFormatterTest extends \PHPUnit_Framework_TestCase $this->assertArrayHasKey('EXTkey', $message_array); $this->assertEquals('pair', $message_array['EXTkey']); } + + public function testFormatWithApplicationName() + { + $formatter = new LogstashFormatter('test', 'app'); + $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']); + } }