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

Merge remote-tracking branch 'mlehner/gelf'

This commit is contained in:
Jordi Boggiano
2012-04-22 16:55:08 +02:00
8 changed files with 435 additions and 10 deletions

5
.gitignore vendored
View File

@@ -1 +1,4 @@
phpunit.xml vendor
composer.phar
phpunit.xml
composer.lock

View File

@@ -4,4 +4,8 @@ php:
- 5.3 - 5.3
- 5.4 - 5.4
before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --dev
script: phpunit script: phpunit

View File

@@ -15,6 +15,12 @@
"require": { "require": {
"php": ">=5.3.0" "php": ">=5.3.0"
}, },
"require-dev": {
"mlehner/gelf-php": "dev-master"
},
"suggest": {
"mlehner/gelf-php": "Allow sending log messages to a GrayLog2 server"
},
"autoload": { "autoload": {
"psr-0": {"Monolog": "src/"} "psr-0": {"Monolog": "src/"}
} }

View File

@@ -0,0 +1,102 @@
<?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 Gelf\Message;
/**
* Serializes a log message according to Wildfire's header requirements
*
* @author Matt Lehner <mlehner@gmail.com>
*/
class GelfMessageFormatter implements FormatterInterface
{
/**
* @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;
/**
* Translates Monolog log levels to Graylog2 log priorities.
*/
private $logLevels = array(
Logger::DEBUG => LOG_DEBUG,
Logger::INFO => LOG_INFO,
Logger::WARNING => LOG_WARNING,
Logger::ERROR => LOG_ERR,
Logger::CRITICAL => LOG_CRIT,
Logger::ALERT => LOG_ALERT,
);
public function __construct($systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_')
{
$this->systemName = $systemName ?: gethostname();
$this->extraPrefix = $extraPrefix;
$this->contextPrefix = $contextPrefix;
}
/**
* {@inheritdoc}
*/
public function format(array $record)
{
$message = new Message();
$message
->setTimestamp($record['datetime']->format('U.u'))
->setShortMessage((string) $record['message'])
->setFacility($record['channel'])
->setHost($this->systemName)
->setLine(isset($record['extra']['line']) ? $record['extra']['line'] : null)
->setFile(isset($record['extra']['file']) ? $record['extra']['file'] : null)
->setLevel($this->logLevels[ $record['level'] ]);
// Do not duplicate these values in the additional fields
unset($record['extra']['line']);
unset($record['extra']['file']);
foreach ($record['extra'] as $key => $val) {
$message->setAdditional($this->extraPrefix . $key, is_scalar($val) ? $val : json_encode($val));
}
foreach ($record['context'] as $key => $val) {
$message->setAdditional($this->contextPrefix . $key, is_scalar($val) ? $val : json_encode($val));
}
return $message;
}
/**
* {@inheritdoc}
*/
public function formatBatch(array $records)
{
$messages = array();
foreach ($records as $record) {
$messages[] = $this->format($record);
}
return $messages;
}
}

View File

@@ -0,0 +1,66 @@
<?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\Handler;
use Gelf\IMessagePublisher;
use Monolog\Logger;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Formatter\GelfMessageFormatter;
/**
* Handler to send messages to a Graylog2 (http://www.graylog2.org) server
*
* @author Matt Lehner <mlehner@gmail.com>
*/
class GelfHandler extends AbstractProcessingHandler
{
/**
* @var Gelf\IMessagePublisher the publisher object that sends the message to the server
*/
protected $publisher;
/**
* @param Gelf\IMessagePublisher $publisher a publisher object
* @param integer $level The minimum logging level at which this handler will be triggered
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
*/
public function __construct(IMessagePublisher $publisher, $level = Logger::DEBUG, $bubble = true)
{
parent::__construct($level, $bubble);
$this->publisher = $publisher;
}
/**
* {@inheritdoc}
*/
public function close()
{
$this->publisher = null;
}
/**
* {@inheritdoc}
*/
protected function write(array $record)
{
$this->publisher->publish($record['formatted']);
}
/**
* {@inheritDoc}
*/
protected function getDefaultFormatter()
{
return new GelfMessageFormatter();
}
}

View File

@@ -0,0 +1,150 @@
<?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;
class GelfMessageFormatterTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Monolog\Formatter\GelfMessageFormatter::format
*/
public function testDefaultFormatter()
{
$formatter = new GelfMessageFormatter();
$record = array(
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => array(),
'datetime' => new \DateTime("@0"),
'extra' => array(),
'message' => 'log',
);
$message = $formatter->format($record);
$this->assertInstanceOf('Gelf\Message', $message);
$this->assertEquals(0, $message->getTimestamp());
$this->assertEquals('log', $message->getShortMessage());
$this->assertEquals('meh', $message->getFacility());
$this->assertEquals(null, $message->getLine());
$this->assertEquals(null, $message->getFile());
$this->assertEquals(LOG_ERR, $message->getLevel());
$this->assertNotEmpty($message->getHost());
$formatter = new GelfMessageFormatter('mysystem');
$message = $formatter->format($record);
$this->assertInstanceOf('Gelf\Message', $message);
$this->assertEquals('mysystem', $message->getHost());
}
/**
* @covers Monolog\Formatter\GelfMessageFormatter::format
*/
public function testFormatWithFileAndLine()
{
$formatter = new GelfMessageFormatter();
$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 = $formatter->format($record);
$this->assertInstanceOf('Gelf\Message', $message);
$this->assertEquals('test', $message->getFile());
$this->assertEquals(14, $message->getLine());
}
/**
* @covers Monolog\Formatter\GelfMessageFormatter::format
*/
public function testFormatWithContext()
{
$formatter = new GelfMessageFormatter();
$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 = $formatter->format($record);
$this->assertInstanceOf('Gelf\Message', $message);
$message_array = $message->toArray();
$this->assertArrayHasKey('_ctxt_from', $message_array);
$this->assertEquals('logger', $message_array['_ctxt_from']);
// Test with extraPrefix
$formatter = new GelfMessageFormatter(null, null, 'CTX');
$message = $formatter->format($record);
$this->assertInstanceOf('Gelf\Message', $message);
$message_array = $message->toArray();
$this->assertArrayHasKey('_CTXfrom', $message_array);
$this->assertEquals('logger', $message_array['_CTXfrom']);
}
/**
* @covers Monolog\Formatter\GelfMessageFormatter::format
*/
public function testFormatWithExtra()
{
$formatter = new GelfMessageFormatter();
$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 = $formatter->format($record);
$this->assertInstanceOf('Gelf\Message', $message);
$message_array = $message->toArray();
$this->assertArrayHasKey('_key', $message_array);
$this->assertEquals('pair', $message_array['_key']);
// Test with extraPrefix
$formatter = new GelfMessageFormatter(null, 'EXT');
$message = $formatter->format($record);
$this->assertInstanceOf('Gelf\Message', $message);
$message_array = $message->toArray();
$this->assertArrayHasKey('_EXTkey', $message_array);
$this->assertEquals('pair', $message_array['_EXTkey']);
}
}

View File

@@ -0,0 +1,102 @@
<?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\Handler;
use Monolog\TestCase;
use Monolog\Logger;
use Monolog\Formatter\GelfMessageFormatter;
use Gelf\MessagePublisher;
use Gelf\Message;
class MockMessagePublisher extends MessagePublisher
{
public function publish(Message $message) {
$this->lastMessage = $message;
}
public $lastMessage = null;
}
class GelfHandlerTest extends TestCase
{
public function setUp()
{
if (!class_exists("Gelf\MessagePublisher")) {
$this->markTestSkipped("mlehner/gelf-php not installed");
}
}
/**
* @covers Monolog\Handler\GelfHandler::__construct
*/
public function testConstruct()
{
$handler = new GelfHandler($this->getMessagePublisher());
$this->assertInstanceOf('Monolog\Handler\GelfHandler', $handler);
}
protected function getHandler($messagePublisher)
{
$handler = new GelfHandler($messagePublisher);
return $handler;
}
protected function getMessagePublisher()
{
return new MockMessagePublisher('localhost');
}
public function testDebug()
{
$messagePublisher = $this->getMessagePublisher();
$handler = $this->getHandler($messagePublisher);
$record = $this->getRecord(Logger::DEBUG, "A test debug message");
$handler->handle($record);
$this->assertEquals(7, $messagePublisher->lastMessage->getLevel());
$this->assertEquals('test', $messagePublisher->lastMessage->getFacility());
$this->assertEquals($record['message'], $messagePublisher->lastMessage->getShortMessage());
$this->assertEquals(null, $messagePublisher->lastMessage->getFullMessage());
}
public function testWarning()
{
$messagePublisher = $this->getMessagePublisher();
$handler = $this->getHandler($messagePublisher);
$record = $this->getRecord(Logger::WARNING, "A test warning message");
$handler->handle($record);
$this->assertEquals(4, $messagePublisher->lastMessage->getLevel());
$this->assertEquals('test', $messagePublisher->lastMessage->getFacility());
$this->assertEquals($record['message'], $messagePublisher->lastMessage->getShortMessage());
$this->assertEquals(null, $messagePublisher->lastMessage->getFullMessage());
}
public function testInjectedGelfMessageFormatter()
{
$messagePublisher = $this->getMessagePublisher();
$handler = $this->getHandler($messagePublisher);
$handler->setFormatter(new GelfMessageFormatter('mysystem', 'EXT', 'CTX'));
$record = $this->getRecord(Logger::WARNING, "A test warning message");
$record['extra']['blarg'] = 'yep';
$record['context']['from'] = 'logger';
$handler->handle($record);
$this->assertEquals('mysystem', $messagePublisher->lastMessage->getHost());
$this->assertArrayHasKey('_EXTblarg', $messagePublisher->lastMessage->toArray());
$this->assertArrayHasKey('_CTXfrom', $messagePublisher->lastMessage->toArray());
}
}

View File

@@ -9,13 +9,5 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
require_once __DIR__ . "/../vendor/autoload.php";
require_once __DIR__.'/Monolog/TestCase.php'; require_once __DIR__.'/Monolog/TestCase.php';
spl_autoload_register(function($class)
{
$file = __DIR__.'/../src/'.strtr($class, '\\', '/').'.php';
if (file_exists($file)) {
require $file;
return true;
}
});