mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-05 12:47:39 +02:00
Make some changes suggested by @stof in
https://github.com/Seldaek/monolog/pull/61
This commit is contained in:
@@ -15,6 +15,15 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.3.0"
|
"php": ">=5.3.0"
|
||||||
},
|
},
|
||||||
|
"repositories": [
|
||||||
|
{
|
||||||
|
"type": "vcs",
|
||||||
|
"url": "https://github.com/mlehner/gelf-php"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"suggest": {
|
||||||
|
"gelf-php": ">=0"
|
||||||
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-0": {"Monolog": "src/"}
|
"psr-0": {"Monolog": "src/"}
|
||||||
}
|
}
|
||||||
|
@@ -1,11 +1,18 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Tellecore\Monolog\Handler;
|
/*
|
||||||
|
* 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\Message;
|
use Gelf\Message;
|
||||||
use Gelf\MessagePublisher;
|
use Gelf\MessagePublisher;
|
||||||
|
|
||||||
use Monolog\Formatter\SimpleFormatter;
|
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use Monolog\Handler\AbstractProcessingHandler;
|
use Monolog\Handler\AbstractProcessingHandler;
|
||||||
|
|
||||||
@@ -16,25 +23,25 @@ use Monolog\Handler\AbstractProcessingHandler;
|
|||||||
*/
|
*/
|
||||||
class GelfHandler extends AbstractProcessingHandler
|
class GelfHandler extends AbstractProcessingHandler
|
||||||
{
|
{
|
||||||
/*
|
/**
|
||||||
* @var Gelf\MessagePublisher the publisher object that sends the message to the server
|
* @var Gelf\MessagePublisher the publisher object that sends the message to the server
|
||||||
*/
|
*/
|
||||||
protected $publisher;
|
protected $publisher;
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* @var string the name of the system for the Gelf log message
|
* @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)
|
* @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)
|
* @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.
|
* Translates Monolog log levels to Graylog2 log priorities.
|
||||||
@@ -50,21 +57,22 @@ class GelfHandler extends AbstractProcessingHandler
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Gelf\MessagePublisher $publisher a publisher object
|
* @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 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 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
|
* @param string $extraPrefix 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
|
* @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);
|
parent::__construct($level, $bubble);
|
||||||
|
|
||||||
$this->publisher = $publisher;
|
$this->publisher = $publisher;
|
||||||
$this->system_name = $system_name ?: gethostname();
|
$this->systemName = $systemName ?: gethostname();
|
||||||
|
|
||||||
$this->extra_prefix = $extra_prefix;
|
$this->extraPrefix = $extraPrefix;
|
||||||
$this->context_prefix = $context_prefix;
|
$this->contentPrefix = $contentPrefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -86,7 +94,7 @@ class GelfHandler extends AbstractProcessingHandler
|
|||||||
->setShortMessage((string) $record['message'])
|
->setShortMessage((string) $record['message'])
|
||||||
->setFullMessage((string) $record['formatted'])
|
->setFullMessage((string) $record['formatted'])
|
||||||
->setFacility($record['channel'])
|
->setFacility($record['channel'])
|
||||||
->setHost($this->system_name)
|
->setHost($this->systemName)
|
||||||
->setLine(isset($record['extra']['line']) ? $record['extra']['line'] : null)
|
->setLine(isset($record['extra']['line']) ? $record['extra']['line'] : null)
|
||||||
->setFile(isset($record['extra']['file']) ? $record['extra']['file'] : null)
|
->setFile(isset($record['extra']['file']) ? $record['extra']['file'] : null)
|
||||||
->setLevel($this->logLevels[ $record['level'] ]);
|
->setLevel($this->logLevels[ $record['level'] ]);
|
||||||
@@ -95,14 +103,12 @@ class GelfHandler extends AbstractProcessingHandler
|
|||||||
unset($record['extra']['line']);
|
unset($record['extra']['line']);
|
||||||
unset($record['extra']['file']);
|
unset($record['extra']['file']);
|
||||||
|
|
||||||
foreach ($record['extra'] as $key => $val)
|
foreach ($record['extra'] as $key => $val) {
|
||||||
{
|
$message->setAdditional($this->extraPrefix . $key, is_scalar($val) ? $val : json_encode($val));
|
||||||
$message->setAdditional($this->extra_prefix . $key, is_scalar($val) ? $val : json_encode($val));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($record['context'] as $key => $val)
|
foreach ($record['context'] as $key => $val) {
|
||||||
{
|
$message->setAdditional($this->contentPrefix . $key, is_scalar($val) ? $val : json_encode($val));
|
||||||
$message->setAdditional($this->context_prefix . $key, is_scalar($val) ? $val : json_encode($val));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->publisher->publish($message);
|
$this->publisher->publish($message);
|
||||||
|
40
tests/Monolog/Handler/GelfHandlerTest.php
Normal file
40
tests/Monolog/Handler/GelfHandlerTest.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?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\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();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user