diff --git a/.gitignore b/.gitignore index 57be4e31..bebd336c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ +vendor +composer.phar phpunit.xml \ No newline at end of file diff --git a/composer.json b/composer.json index a6e4954c..c60dc301 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,23 @@ "require": { "php": ">=5.3.0" }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/mlehner/gelf-php" + }, + { + "type": "vcs", + "url": "https://github.com/msabramo/gelf-php-1" + }, + { + "type": "vcs", + "url": "https://github.com/msabramo/gelf-php" + } + ], + "suggest": { + "mlehner/gelf-php": "dev-add-composer-support" + }, "autoload": { "psr-0": {"Monolog": "src/"} } diff --git a/composer.lock b/composer.lock new file mode 100644 index 00000000..034897c8 --- /dev/null +++ b/composer.lock @@ -0,0 +1,11 @@ +{ + "hash": "5715c4c52bc66b23002954fb3a2f47e4", + "packages": [ + { + "package": "mlehner/gelf-php", + "version": "dev-add-composer-support", + "source-reference": "39419c2b75daf6b9eb9feb6ac0541807ee888cda" + } + ], + "aliases": [] +} diff --git a/src/Monolog/Handler/GelfHandler.php b/src/Monolog/Handler/GelfHandler.php index 3fcf9009..ad4aaabe 100644 --- a/src/Monolog/Handler/GelfHandler.php +++ b/src/Monolog/Handler/GelfHandler.php @@ -1,11 +1,18 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Handler; use Gelf\Message; use Gelf\MessagePublisher; - -use Monolog\Formatter\SimpleFormatter; use Monolog\Logger; use Monolog\Handler\AbstractProcessingHandler; @@ -16,25 +23,25 @@ use Monolog\Handler\AbstractProcessingHandler; */ class GelfHandler extends AbstractProcessingHandler { - /* + /** * @var Gelf\MessagePublisher the publisher object that sends the message to the server */ protected $publisher; - /* + /** * @var string the name of the system for the Gelf log message */ - protected $system_name; + protected $systemName; - /* + /** * @var string a prefix for 'extra' fields from the Monolog record (optional) */ - protected $extra_prefix; + protected $extraPrefix; - /* + /** * @var string a prefix for 'context' fields from the Monolog record (optional) */ - protected $context_prefix; + protected $contentPrefix; /** * Translates Monolog log levels to Graylog2 log priorities. @@ -50,21 +57,22 @@ class GelfHandler extends AbstractProcessingHandler /** * @param Gelf\MessagePublisher $publisher a publisher object - * @param string $system_name the name of the system sending messages + * @param string $systemName the name of the system sending messages * @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 - * @param string $extra_prefix a string to prefix for all the 'extra' fields from Monolog record - * @oaram string $context_prefix a string to prefix for all the 'context' fields from a Monolog record + * @param string $extraPrefix a string to prefix for all the 'extra' fields from Monolog record + * @oaram string $contentPrefix a string to prefix for all the 'context' fields from a Monolog record */ - public function __construct(MessagePublisher $publisher, $system_name = null, $level = Logger::DEBUG, $bubble = true, $extra_prefix = null, $context_prefix = null) + public function __construct(MessagePublisher $publisher, $systemName = null, $level = Logger::DEBUG, + $bubble = true, $extraPrefix = null, $contentPrefix = 'ctxt_') { parent::__construct($level, $bubble); $this->publisher = $publisher; - $this->system_name = $system_name ?: gethostname(); + $this->systemName = $systemName ?: gethostname(); - $this->extra_prefix = $extra_prefix; - $this->context_prefix = $context_prefix; + $this->extraPrefix = $extraPrefix; + $this->contentPrefix = $contentPrefix; } /** @@ -86,7 +94,7 @@ class GelfHandler extends AbstractProcessingHandler ->setShortMessage((string) $record['message']) ->setFullMessage((string) $record['formatted']) ->setFacility($record['channel']) - ->setHost($this->system_name) + ->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'] ]); @@ -95,14 +103,12 @@ class GelfHandler extends AbstractProcessingHandler unset($record['extra']['line']); unset($record['extra']['file']); - foreach ($record['extra'] as $key => $val) - { - $message->setAdditional($this->extra_prefix . $key, is_scalar($val) ? $val : json_encode($val)); + 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->context_prefix . $key, is_scalar($val) ? $val : json_encode($val)); + foreach ($record['context'] as $key => $val) { + $message->setAdditional($this->contentPrefix . $key, is_scalar($val) ? $val : json_encode($val)); } $this->publisher->publish($message); diff --git a/tests/Monolog/Handler/GelfHandlerTest.php b/tests/Monolog/Handler/GelfHandlerTest.php new file mode 100644 index 00000000..81a1ca15 --- /dev/null +++ b/tests/Monolog/Handler/GelfHandlerTest.php @@ -0,0 +1,86 @@ + + * + * 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 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("https://github.com/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); + $handler->setFormatter($this->getIdentityFormatter()); + 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($record['message'], $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($record['message'], $messagePublisher->lastMessage->getFullMessage()); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index e3610ebc..dc92e51d 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -9,6 +9,7 @@ * file that was distributed with this source code. */ +require_once __DIR__ . "/../vendor/.composer/autoload.php"; require_once __DIR__.'/Monolog/TestCase.php'; spl_autoload_register(function($class)