From c9e61750d096240701129cf0577f75c6b1fb2ef3 Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Thu, 5 Apr 2012 22:40:07 -0700 Subject: [PATCH 1/3] Make some changes suggested by @stof in https://github.com/Seldaek/monolog/pull/61 --- composer.json | 9 ++++ src/Monolog/Handler/GelfHandler.php | 54 +++++++++++++---------- tests/Monolog/Handler/GelfHandlerTest.php | 40 +++++++++++++++++ 3 files changed, 79 insertions(+), 24 deletions(-) create mode 100644 tests/Monolog/Handler/GelfHandlerTest.php diff --git a/composer.json b/composer.json index a6e4954c..7585d498 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,15 @@ "require": { "php": ">=5.3.0" }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/mlehner/gelf-php" + } + ], + "suggest": { + "gelf-php": ">=0" + }, "autoload": { "psr-0": {"Monolog": "src/"} } 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..1633bd29 --- /dev/null +++ b/tests/Monolog/Handler/GelfHandlerTest.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Handler; + +use Monolog\Logger; +use Gelf\MessagePublisher; + +class GelfHandlerTest extends \PHPUnit_Framework_TestCase +{ + public function setUp() + { + if (!class_exists("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 getMessagePublisher() + { + return new MessagePublisher(); + } +} From 14ac76a0aefee79dfd4a9045f594a731a959c8bb Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Fri, 6 Apr 2012 07:25:02 -0700 Subject: [PATCH 2/3] Pull in mlehner/gelf-php via Composer --- .gitignore | 2 ++ composer.json | 10 +++++++++- composer.lock | 11 +++++++++++ tests/Monolog/Handler/GelfHandlerTest.php | 15 ++++++++++++--- tests/bootstrap.php | 1 + 5 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 composer.lock 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 7585d498..c60dc301 100644 --- a/composer.json +++ b/composer.json @@ -19,10 +19,18 @@ { "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": { - "gelf-php": ">=0" + "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/tests/Monolog/Handler/GelfHandlerTest.php b/tests/Monolog/Handler/GelfHandlerTest.php index 1633bd29..cfd2f640 100644 --- a/tests/Monolog/Handler/GelfHandlerTest.php +++ b/tests/Monolog/Handler/GelfHandlerTest.php @@ -11,14 +11,15 @@ namespace Monolog\Handler; +use Monolog\TestCase; use Monolog\Logger; use Gelf\MessagePublisher; -class GelfHandlerTest extends \PHPUnit_Framework_TestCase +class GelfHandlerTest extends TestCase { public function setUp() { - if (!class_exists("MessagePublisher")) + if (!class_exists("Gelf\MessagePublisher")) { $this->markTestSkipped("https://github.com/mlehner/gelf-php not installed"); } @@ -35,6 +36,14 @@ class GelfHandlerTest extends \PHPUnit_Framework_TestCase protected function getMessagePublisher() { - return new MessagePublisher(); + return new MessagePublisher('localhost'); + } + + public function testStuff() + { + $handler = new GelfHandler($this->getMessagePublisher()); + $handler->setFormatter($this->getIdentityFormatter()); + $handler->handle($this->getRecord(Logger::DEBUG)); + $handler->handle($this->getRecord(Logger::WARNING)); } } 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) From bd28443b730895a6a3667d07c781e231f44d85d4 Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Fri, 6 Apr 2012 08:30:26 -0700 Subject: [PATCH 3/3] Test using a MockMessagePublisher --- tests/Monolog/Handler/GelfHandlerTest.php | 51 +++++++++++++++++++---- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/tests/Monolog/Handler/GelfHandlerTest.php b/tests/Monolog/Handler/GelfHandlerTest.php index cfd2f640..81a1ca15 100644 --- a/tests/Monolog/Handler/GelfHandlerTest.php +++ b/tests/Monolog/Handler/GelfHandlerTest.php @@ -14,6 +14,16 @@ 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 { @@ -34,16 +44,43 @@ class GelfHandlerTest extends TestCase $this->assertInstanceOf('Monolog\Handler\GelfHandler', $handler); } - protected function getMessagePublisher() + protected function getHandler($messagePublisher) { - return new MessagePublisher('localhost'); + $handler = new GelfHandler($messagePublisher); + $handler->setFormatter($this->getIdentityFormatter()); + return $handler; } - public function testStuff() + protected function getMessagePublisher() { - $handler = new GelfHandler($this->getMessagePublisher()); - $handler->setFormatter($this->getIdentityFormatter()); - $handler->handle($this->getRecord(Logger::DEBUG)); - $handler->handle($this->getRecord(Logger::WARNING)); + 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()); } }