Skip to content

Commit

Permalink
Add logstash formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
timmow committed Oct 16, 2012
1 parent 4b11ac0 commit 93329e2
Show file tree
Hide file tree
Showing 2 changed files with 226 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/Monolog/Formatter/LogstashEventFormatter.php
@@ -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);
}
}
142 changes: 142 additions & 0 deletions tests/Monolog/Formatter/LogstashEventFormatterTest.php
@@ -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']);
}
}

0 comments on commit 93329e2

Please sign in to comment.