1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-01 19:00:20 +02:00

Merge pull request #430 from moderndeveloperllc/master

NewRelicHandler: Account for arrays sent in context and extra parameters
This commit is contained in:
Jordi Boggiano
2014-10-04 12:53:35 +01:00
2 changed files with 56 additions and 6 deletions

View File

@@ -16,7 +16,7 @@ use Monolog\Logger;
/**
* Class to record a log on a NewRelic application
*
* @see https://newrelic.com/docs/php/new-relic-for-php
* @see https://docs.newrelic.com/docs/agents/php-agent
*/
class NewRelicHandler extends AbstractProcessingHandler
{
@@ -27,16 +27,26 @@ class NewRelicHandler extends AbstractProcessingHandler
*/
protected $appName;
/**
* Some context and extra data is passed into the handler as arrays of values. Do we send them as is
* (useful if we are using the API), or explode them for display on the NewRelic RPM website?
*
* @var boolean
*/
protected $explodeArrays;
/**
* {@inheritDoc}
*
* @param string $appName
* @param string $appName
* @param boolean $implodeArrays
*/
public function __construct($level = Logger::ERROR, $bubble = true, $appName = null)
public function __construct($level = Logger::ERROR, $bubble = true, $appName = null, $explodeArrays = false)
{
parent::__construct($level, $bubble);
$this->appName = $appName;
$this->appName = $appName;
$this->explodeArrays = $explodeArrays;
}
/**
@@ -60,11 +70,23 @@ class NewRelicHandler extends AbstractProcessingHandler
}
foreach ($record['context'] as $key => $parameter) {
newrelic_add_custom_parameter('context_' . $key, $parameter);
if (is_array($parameter) && $this->explodeArrays) {
foreach ($parameter as $paramKey => $paramValue) {
newrelic_add_custom_parameter('context_' . $key . '_' . $paramKey, $paramValue);
}
} else {
newrelic_add_custom_parameter('context_' . $key, $parameter);
}
}
foreach ($record['extra'] as $key => $parameter) {
newrelic_add_custom_parameter('extra_' . $key, $parameter);
if (is_array($parameter) && $this->explodeArrays) {
foreach ($parameter as $paramKey => $paramValue) {
newrelic_add_custom_parameter('extra_' . $key . '_' . $paramKey, $paramValue);
}
} else {
newrelic_add_custom_parameter('extra_' . $key, $parameter);
}
}
}

View File

@@ -47,6 +47,20 @@ class NewRelicHandlerTest extends TestCase
$this->assertEquals(array('context_a' => 'b'), self::$customParameters);
}
public function testThehandlerCanAddExplodedContextParamsToTheNewRelicTrace()
{
$handler = new StubNewRelicHandler(Logger::ERROR, true, self::$appname, true);
$handler->handle($this->getRecord(
Logger::ERROR,
'log message',
array('a' => array('key1' => 'value1', 'key2' => 'value2'))
));
$this->assertEquals(
array('context_a_key1' => 'value1', 'context_a_key2' => 'value2'),
self::$customParameters
);
}
public function testThehandlerCanAddExtraParamsToTheNewRelicTrace()
{
$record = $this->getRecord(Logger::ERROR, 'log message');
@@ -58,6 +72,20 @@ class NewRelicHandlerTest extends TestCase
$this->assertEquals(array('extra_c' => 'd'), self::$customParameters);
}
public function testThehandlerCanAddExplodedExtraParamsToTheNewRelicTrace()
{
$record = $this->getRecord(Logger::ERROR, 'log message');
$record['extra'] = array('c' => array('key1' => 'value1', 'key2' => 'value2'));
$handler = new StubNewRelicHandler(Logger::ERROR, true, self::$appname, true);
$handler->handle($record);
$this->assertEquals(
array('extra_c_key1' => 'value1', 'extra_c_key2' => 'value2'),
self::$customParameters
);
}
public function testThehandlerCanAddExtraContextAndParamsToTheNewRelicTrace()
{
$record = $this->getRecord(Logger::ERROR, 'log message', array('a' => 'b'));