Просмотр исходного кода

Merge pull request #228 from odino/master

NewRelic handler: added the app name
Jordi Boggiano 12 лет назад
Родитель
Сommit
933f7e26be

+ 42 - 1
src/Monolog/Handler/NewRelicHandler.php

@@ -20,12 +20,23 @@ use Monolog\Logger;
  */
 class NewRelicHandler extends AbstractProcessingHandler
 {
+    /**
+     * Name of the New Relic application that will receive logs from this handler.
+     *
+     * @var string
+     */
+    protected $appName;
+
     /**
      * {@inheritDoc}
+     *
+     * @param string $appName
      */
-    public function __construct($level = Logger::ERROR, $bubble = true)
+    public function __construct($level = Logger::ERROR, $bubble = true, $appName = null)
     {
         parent::__construct($level, $bubble);
+
+        $this->appName = $appName;
     }
 
     /**
@@ -37,6 +48,10 @@ class NewRelicHandler extends AbstractProcessingHandler
             throw new MissingExtensionException('The newrelic PHP extension is required to use the NewRelicHandler');
         }
 
+        if ($appName = $this->getAppName($record['context'])) {
+            $this->setNewRelicAppName($appName);
+        }
+
         if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Exception) {
             newrelic_notice_error($record['message'], $record['context']['exception']);
             unset($record['context']['exception']);
@@ -58,4 +73,30 @@ class NewRelicHandler extends AbstractProcessingHandler
     {
         return extension_loaded('newrelic');
     }
+
+    /**
+     * Returns the appname where this log should be sent. Each log can override the default appname, set in this
+     * handler's constructor, by providing the appname in its context.
+     *
+     * @param array $context
+     * @return null|string
+     */
+    protected function getAppName(array $context)
+    {
+        if (isset($context['appname'])) {
+            return $context['appname'];
+        }
+
+        return $this->appName;
+    }
+
+    /**
+     * Sets the NewRelic application that should receive this log.
+     *
+     * @param string $appName
+     */
+    protected function setNewRelicAppName($appName)
+    {
+        newrelic_set_appname($appName);
+    }
 }

+ 37 - 0
tests/Monolog/Handler/NewRelicHandlerTest.php

@@ -13,9 +13,17 @@ namespace Monolog\Handler;
 
 use Monolog\TestCase;
 use Monolog\Logger;
+use Psr\Log\LogLevel;
 
 class NewRelicHandlerTest extends TestCase
 {
+    public static $appname;
+
+    public function setUp()
+    {
+        $this::$appname = null;
+    }
+
     /**
      * @expectedException Monolog\Handler\MissingExtensionException
      */
@@ -36,6 +44,30 @@ class NewRelicHandlerTest extends TestCase
         $handler = new StubNewRelicHandler();
         $handler->handle($this->getRecord(Logger::ERROR, 'log message', array('a' => 'b')));
     }
+
+    public function testTheAppNameIsNullByDefault()
+    {
+        $handler = new StubNewRelicHandler();
+        $handler->handle($this->getRecord(Logger::ERROR, 'log message'));
+
+        $this->assertEquals(null, $this::$appname);
+    }
+
+    public function testTheAppNameCanBeInjectedFromtheConstructor()
+    {
+        $handler = new StubNewRelicHandler(LogLevel::ALERT, false, 'myAppName');
+        $handler->handle($this->getRecord(Logger::ERROR, 'log message'));
+
+        $this->assertEquals('myAppName', $this::$appname);
+    }
+
+    public function testTheAppNameCanBeOverriddenFromEachLog()
+    {
+        $handler = new StubNewRelicHandler(LogLevel::ALERT, false, 'myAppName');
+        $handler->handle($this->getRecord(Logger::ERROR, 'log message', array('appname' => 'logAppName')));
+
+        $this->assertEquals('logAppName', $this::$appname);
+    }
 }
 
 class StubNewRelicHandlerWithoutExtension extends NewRelicHandler
@@ -59,6 +91,11 @@ function newrelic_notice_error()
     return true;
 }
 
+function newrelic_set_appname($appname)
+{
+    return NewRelicHandlerTest::$appname = $appname;
+}
+
 function newrelic_add_custom_parameter()
 {
     return true;