Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Monolog package. | ||
* | ||
* (c) Jordi Boggiano <j.boggiano@seld.be> | ||
* | ||
* 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; | ||
|
||
/** | ||
* 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 <timothy.mower@gmail.com> | ||
*/ | ||
class LogstashEventFormatter extends NormalizerFormatter | ||
{ | ||
/** | ||
* @var string the name of the system for the Gelf log message | ||
*/ | ||
protected $systemName; | ||
|
||
/** | ||
* @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; | ||
|
||
|
||
public function __construct($systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_') | ||
{ | ||
//log stash requires a ISO 8601 format date | ||
parent::__construct('c'); | ||
|
||
$this->systemName = $systemName ?: gethostname(); | ||
|
||
$this->extraPrefix = $extraPrefix; | ||
$this->contextPrefix = $contextPrefix; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function format(array $record) | ||
{ | ||
$record = parent::format($record); | ||
$message = array( | ||
'@timestamp' => $record['datetime'], | ||
'@message' => $record['message'], | ||
'@tags' => array($record['channel']), | ||
'@source' => $this->systemName | ||
); | ||
|
||
$message['@fields'] = array(); | ||
$message['@fields']['channel'] = $record['channel']; | ||
$message['@fields']['level'] = $record['level']; | ||
if (isset($record['extra']['server'])) { | ||
$message['@source_host'] = $record['extra']['server']; | ||
} | ||
if (isset($record['extra']['url'])) { | ||
$message['@source_path'] = $record['extra']['url']; | ||
} | ||
foreach ($record['extra'] as $key => $val) { | ||
$message['@fields'][$this->extraPrefix . $key] = $val; | ||
} | ||
|
||
foreach ($record['context'] as $key => $val) { | ||
$message['@fields'][$this->contextPrefix . $key] = $val; | ||
} | ||
|
||
return json_encode($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Monolog package. | ||
* | ||
* (c) Jordi Boggiano <j.boggiano@seld.be> | ||
* | ||
* 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\LogstashEventFormatter; | ||
|
||
class LogstashEventFormatterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
/** | ||
* @covers Monolog\Formatter\GelfMessageFormatter::format | ||
*/ | ||
public function testDefaultFormatter() | ||
{ | ||
$formatter = new LogstashEventFormatter('test'); | ||
$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('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['@source']); | ||
|
||
$formatter = new GelfMessageFormatter('mysystem'); | ||
|
||
$message = $formatter->format($record); | ||
|
||
$this->assertEquals('mysystem', $message->getHost()); | ||
} | ||
|
||
/** | ||
* @covers Monolog\Formatter\GelfMessageFormatter::format | ||
*/ | ||
public function testFormatWithFileAndLine() | ||
{ | ||
$formatter = new LogstashEventFormatter('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\GelfMessageFormatter::format | ||
*/ | ||
public function testFormatWithContext() | ||
{ | ||
$formatter = new LogstashEventFormatter('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 LogstashEventFormatter('test', 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\GelfMessageFormatter::format | ||
*/ | ||
public function testFormatWithExtra() | ||
{ | ||
$formatter = new LogstashEventFormatter('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 LogstashEventFormatter('test', 'EXT'); | ||
$message = json_decode($formatter->format($record), true); | ||
|
||
$message_array = $message['@fields']; | ||
|
||
$this->assertArrayHasKey('EXTkey', $message_array); | ||
$this->assertEquals('pair', $message_array['EXTkey']); | ||
} | ||
} |