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(); + } +}