mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-07 21:56:31 +02:00
refactored the implementation of the NR handler and its test:
* the test now checks that if the extension is not loaded, an exception is thrown * the test now mocks the new relic native functions * moved some parameters as constants in the handler class
This commit is contained in:
@@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Monolog\Handler;
|
namespace Monolog\Handler;
|
||||||
|
|
||||||
|
use Monolog\Handler\MissingExtensionException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class to record a log on a NewRelic application
|
* Class to record a log on a NewRelic application
|
||||||
*
|
*
|
||||||
@@ -18,17 +20,34 @@ namespace Monolog\Handler;
|
|||||||
*/
|
*/
|
||||||
class NewRelicHandler extends AbstractProcessingHandler
|
class NewRelicHandler extends AbstractProcessingHandler
|
||||||
{
|
{
|
||||||
|
const ERROR_MISSING_EXTENSION = "The NewRelic PHP extension is not installed on this system, therefore you can't use the NewRelicHandler";
|
||||||
|
const NEWRELIC_EXTENSION_NAME = 'newrelic';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
protected function write(array $record)
|
protected function write(array $record)
|
||||||
{
|
{
|
||||||
if (extension_loaded('newrelic')) {
|
if ($this->isNewRelicEnabled()) {
|
||||||
newrelic_notice_error($record['message']);
|
newrelic_notice_error($record['message']);
|
||||||
|
|
||||||
foreach ($record['context'] as $key => $parameter) {
|
foreach ($record['context'] as $key => $parameter) {
|
||||||
newrelic_add_custom_parameter($key, $parameter);
|
newrelic_add_custom_parameter($key, $parameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new MissingExtensionException(self::ERROR_MISSING_EXTENSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the NewRelic extension is enabled in the system.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function isNewRelicEnabled()
|
||||||
|
{
|
||||||
|
return (bool) extension_loaded(self::NEWRELIC_EXTENSION_NAME);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -15,18 +15,49 @@ use Monolog\TestCase;
|
|||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
|
||||||
class NewRelicHandlerTest extends TestCase
|
class NewRelicHandlerTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testFallbackHandler()
|
/**
|
||||||
|
* @expectedException Monolog\Handler\MissingExtensionException
|
||||||
|
*/
|
||||||
|
public function testThehandlerThrowsAnExceptionIfTheNRExtensionIsNotLoaded()
|
||||||
{
|
{
|
||||||
$handler = new NewRelicHandler();
|
$handler = new StubNewRelicHandlerWithoutExtension();
|
||||||
$fallbackHandler = new TestHandler();
|
$handler->handle($this->getRecord());
|
||||||
$record = array(
|
}
|
||||||
'level' => Logger::DEBUG,
|
|
||||||
'extra' => array(),
|
public function testThehandlerCanHandleTheRecord()
|
||||||
);
|
{
|
||||||
|
$handler = new StubNewRelicHandler();
|
||||||
$handler->handle($record);
|
$handler->handle($this->getRecord());
|
||||||
|
}
|
||||||
$this->assertTrue($handler->isHandling($record));
|
|
||||||
|
public function testThehandlerCanAddParamsToTheNewRelicTrace()
|
||||||
|
{
|
||||||
|
$handler = new StubNewRelicHandler();
|
||||||
|
$handler->handle($this->getRecord(100, 'log message', array('a' => 'b')));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class StubNewRelicHandlerWithoutExtension extends NewRelicHandler
|
||||||
|
{
|
||||||
|
protected function isNewRelicEnabled()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class StubNewRelicHandler extends NewRelicHandler
|
||||||
|
{
|
||||||
|
protected function isNewRelicEnabled()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function newrelic_notice_error() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function newrelic_add_custom_parameter() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user