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

PsrLogMessageProcessor: add option to remove used context fields

Jakub Chábek 8 лет назад
Родитель
Сommit
009d4151b4

+ 19 - 5
src/Monolog/Processor/PsrLogMessageProcessor.php

@@ -24,12 +24,17 @@ class PsrLogMessageProcessor
 
 
     private $dateFormat;
     private $dateFormat;
 
 
+    /** @var bool */
+    private $removeUsedContextFields;
+
     /**
     /**
      * @param string $dateFormat The format of the timestamp: one supported by DateTime::format
      * @param string $dateFormat The format of the timestamp: one supported by DateTime::format
+     * @param bool $removeUsedContextFields If set to true the fields interpolated into message gets unset
      */
      */
-    public function __construct(string $dateFormat = null)
+    public function __construct(string $dateFormat = null, bool $removeUsedContextFields = false)
     {
     {
         $this->dateFormat = null === $dateFormat ? static::SIMPLE_DATE : $dateFormat;
         $this->dateFormat = null === $dateFormat ? static::SIMPLE_DATE : $dateFormat;
+        $this->removeUsedContextFields = $removeUsedContextFields;
     }
     }
 
 
     /**
     /**
@@ -44,14 +49,23 @@ class PsrLogMessageProcessor
 
 
         $replacements = [];
         $replacements = [];
         foreach ($record['context'] as $key => $val) {
         foreach ($record['context'] as $key => $val) {
+            $placeholder = '{' . $key . '}';
+            if (strpos($record['message'], $placeholder) === false) {
+                continue;
+            }
+
             if (is_null($val) || is_scalar($val) || (is_object($val) && method_exists($val, "__toString"))) {
             if (is_null($val) || is_scalar($val) || (is_object($val) && method_exists($val, "__toString"))) {
-                $replacements['{'.$key.'}'] = $val;
+                $replacements[$placeholder] = $val;
             } elseif ($val instanceof \DateTimeInterface) {
             } elseif ($val instanceof \DateTimeInterface) {
-                $replacements['{'.$key.'}'] = $val->format($this->dateFormat);
+                $replacements[$placeholder] = $val->format($this->dateFormat);
             } elseif (is_object($val)) {
             } elseif (is_object($val)) {
-                $replacements['{'.$key.'}'] = '[object '.get_class($val).']';
+                $replacements[$placeholder] = '[object '.get_class($val).']';
             } else {
             } else {
-                $replacements['{'.$key.'}'] = '['.gettype($val).']';
+                $replacements[$placeholder] = '['.gettype($val).']';
+            }
+
+            if ($this->removeUsedContextFields) {
+                unset($record['context'][$key]);
             }
             }
         }
         }
 
 

+ 14 - 0
tests/Monolog/Processor/PsrLogMessageProcessorTest.php

@@ -25,6 +25,19 @@ class PsrLogMessageProcessorTest extends \PHPUnit\Framework\TestCase
             'context' => ['foo' => $val],
             'context' => ['foo' => $val],
         ]);
         ]);
         $this->assertEquals($expected, $message['message']);
         $this->assertEquals($expected, $message['message']);
+        $this->assertSame(['foo' => $val], $message['context']);
+    }
+
+    public function testReplacementWithContextRemoval()
+    {
+        $proc = new PsrLogMessageProcessor($dateFormat = null, $removeUsedContextFields = true);
+
+        $message = $proc([
+            'message' => '{foo}',
+            'context' => ['foo' => 'bar', 'lorem' => 'ipsum'],
+        ]);
+        $this->assertSame('bar', $message['message']);
+        $this->assertSame(['lorem' => 'ipsum'], $message['context']);
     }
     }
 
 
     public function testCustomDateFormat()
     public function testCustomDateFormat()
@@ -39,6 +52,7 @@ class PsrLogMessageProcessorTest extends \PHPUnit\Framework\TestCase
             'context' => ['foo' => $date],
             'context' => ['foo' => $date],
         ]);
         ]);
         $this->assertEquals($date->format($format), $message['message']);
         $this->assertEquals($date->format($format), $message['message']);
+        $this->assertSame(['foo' => $date], $message['context']);
     }
     }
 
 
     public function getPairs()
     public function getPairs()