mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-02 11:20:31 +02:00
Merge pull request #430 from moderndeveloperllc/master
NewRelicHandler: Account for arrays sent in context and extra parameters
This commit is contained in:
@@ -16,7 +16,7 @@ use Monolog\Logger;
|
|||||||
/**
|
/**
|
||||||
* Class to record a log on a NewRelic application
|
* 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
|
class NewRelicHandler extends AbstractProcessingHandler
|
||||||
{
|
{
|
||||||
@@ -27,16 +27,26 @@ class NewRelicHandler extends AbstractProcessingHandler
|
|||||||
*/
|
*/
|
||||||
protected $appName;
|
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}
|
* {@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);
|
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) {
|
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) {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -47,6 +47,20 @@ class NewRelicHandlerTest extends TestCase
|
|||||||
$this->assertEquals(array('context_a' => 'b'), self::$customParameters);
|
$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()
|
public function testThehandlerCanAddExtraParamsToTheNewRelicTrace()
|
||||||
{
|
{
|
||||||
$record = $this->getRecord(Logger::ERROR, 'log message');
|
$record = $this->getRecord(Logger::ERROR, 'log message');
|
||||||
@@ -58,6 +72,20 @@ class NewRelicHandlerTest extends TestCase
|
|||||||
$this->assertEquals(array('extra_c' => 'd'), self::$customParameters);
|
$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()
|
public function testThehandlerCanAddExtraContextAndParamsToTheNewRelicTrace()
|
||||||
{
|
{
|
||||||
$record = $this->getRecord(Logger::ERROR, 'log message', array('a' => 'b'));
|
$record = $this->getRecord(Logger::ERROR, 'log message', array('a' => 'b'));
|
||||||
|
Reference in New Issue
Block a user