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

Added a request token processor to detect log messages per request

Simon Mönch 13 лет назад
Родитель
Сommit
bad76ce2a6

+ 1 - 0
README.mdown

@@ -184,6 +184,7 @@ Processors
 - _MemoryUsageProcessor_: Adds the current memory usage to a log record.
 - _MemoryPeakUsageProcessor_: Adds the peak memory usage to a log record.
 - _ProcessIdProcessor_: Adds the process id to a log record.
+- _RequestTokenProcessor_: Adds a request token to a log record.
 
 About
 =====

+ 36 - 0
src/Monolog/Processor/RequestTokenProcessor.php

@@ -0,0 +1,36 @@
+<?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 request token into records
+ *
+ * @author Simon Mönch <sm@webfactory.de>
+ */
+class RequestTokenProcessor 
+{
+    private static $requestToken;
+    
+    public function __construct() 
+    {
+        if (null === self::$requestToken) {
+            self::$requestToken = substr(hash('md5', uniqid('', true)), 0, 7);
+        }
+    }
+
+    public function __invoke(array $record) 
+    {
+        $record['extra']['request_token'] = self::$requestToken;
+
+        return $record;
+    }
+}

+ 27 - 0
tests/Monolog/Processor/RequestTokenProcessorTest.php

@@ -0,0 +1,27 @@
+<?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 RequestTokenProcessorTest extends TestCase
+{
+    /**
+     * @covers Monolog\Processor\RequestTokenProcessor::__invoke
+     */
+    public function testProcessor()
+    {
+        $processor = new RequestTokenProcessor();
+        $record = $processor($this->getRecord());
+        $this->assertArrayHasKey('request_token', $record['extra']);
+    }
+}