mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-07 21:56:31 +02:00
Merge pull request #1 from msabramo/gelf
Make some changes suggested by @stof
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
|||||||
|
vendor
|
||||||
|
composer.phar
|
||||||
phpunit.xml
|
phpunit.xml
|
@@ -15,6 +15,23 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.3.0"
|
"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": {
|
"autoload": {
|
||||||
"psr-0": {"Monolog": "src/"}
|
"psr-0": {"Monolog": "src/"}
|
||||||
}
|
}
|
||||||
|
11
composer.lock
generated
Normal file
11
composer.lock
generated
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"hash": "5715c4c52bc66b23002954fb3a2f47e4",
|
||||||
|
"packages": [
|
||||||
|
{
|
||||||
|
"package": "mlehner/gelf-php",
|
||||||
|
"version": "dev-add-composer-support",
|
||||||
|
"source-reference": "39419c2b75daf6b9eb9feb6ac0541807ee888cda"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"aliases": []
|
||||||
|
}
|
@@ -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);
|
||||||
|
86
tests/Monolog/Handler/GelfHandlerTest.php
Normal file
86
tests/Monolog/Handler/GelfHandlerTest.php
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
<?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 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());
|
||||||
|
}
|
||||||
|
}
|
@@ -9,6 +9,7 @@
|
|||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
require_once __DIR__ . "/../vendor/.composer/autoload.php";
|
||||||
require_once __DIR__.'/Monolog/TestCase.php';
|
require_once __DIR__.'/Monolog/TestCase.php';
|
||||||
|
|
||||||
spl_autoload_register(function($class)
|
spl_autoload_register(function($class)
|
||||||
|
Reference in New Issue
Block a user