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

Merge pull request #347 from ipsq/master

Added tags for Raven
Jordi Boggiano 11 лет назад
Родитель
Сommit
2dd41709c6

+ 9 - 0
src/Monolog/Handler/RavenHandler.php

@@ -129,6 +129,15 @@ class RavenHandler extends AbstractProcessingHandler
     {
         $options = array();
         $options['level'] = $this->logLevels[$record['level']];
+        $options['tags'] = array();
+        if (!empty($record['extra']['tags'])) {
+            $options['tags'] = array_merge($options['tags'], $record['extra']['tags']);
+            unset($record['extra']['tags']);
+        }
+        if (!empty($record['context']['tags'])) {
+            $options['tags'] = array_merge($options['tags'], $record['context']['tags']);
+            unset($record['context']['tags']);
+        }
         if (!empty($record['context'])) {
             $options['extra']['context'] = $record['context'];
         }

+ 34 - 0
src/Monolog/Processor/TagProcessor.php

@@ -0,0 +1,34 @@
+<?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\Processor;
+
+/**
+ * Adds a tags array into record
+ *
+ * @author Martijn Riemers
+ */
+class TagProcessor
+{
+    private $tags;
+
+    public function __construct(array $tags = array())
+    {
+        $this->tags = $tags;
+    }
+
+    public function __invoke(array $record)
+    {
+        $record['extra']['tags'] = $this->tags;
+
+        return $record;
+    }
+}

+ 12 - 0
tests/Monolog/Handler/RavenHandlerTest.php

@@ -72,6 +72,18 @@ class RavenHandlerTest extends TestCase
         $this->assertEquals($ravenClient::WARNING, $ravenClient->lastData['level']);
         $this->assertContains($record['message'], $ravenClient->lastData['message']);
     }
+    
+    public function testTag()
+    {
+        $ravenClient = $this->getRavenClient();
+        $handler = $this->getHandler($ravenClient);
+
+        $tags = array(1, 2, 'foo');
+        $record = $this->getRecord(Logger::INFO, "test", array('tags' => $tags));
+        $handler->handle($record);
+
+        $this->assertEquals($tags, $ravenClient->lastData['tags']);
+    }
 
     public function testException()
     {

+ 29 - 0
tests/Monolog/Processor/TagProcessorTest.php

@@ -0,0 +1,29 @@
+<?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\Processor;
+
+use Monolog\TestCase;
+
+class TagProcessorTest extends TestCase
+{
+    /**
+     * @covers Monolog\Processor\TagProcessor::__invoke
+     */
+    public function testProcessor()
+    {
+        $tags = array(1, 2, 3);
+        $processor = new TagProcessor($tags);
+        $record = $processor($this->getRecord());
+        
+        $this->assertEquals($tags, $record['extra']['tags']);
+    }
+}