[FEATURE] Add include keys list 44/50444/2
authorFrancois Suter <francois@typo3.org>
Sun, 30 Oct 2016 21:15:25 +0000 (22:15 +0100)
committerFrancois Suter <francois@typo3.org>
Sun, 30 Oct 2016 21:17:46 +0000 (22:17 +0100)
Add option for a list of keys who should be the only ones to be
logged. Exclude keys are ignored if include keys are defined.

Change-Id: I517d7020258a057193f5133eeffe3f7f3d89ebe9
Resolves: #3053
Releases: 3.0
Reviewed-on: https://review.typo3.org/50444
Reviewed-by: Francois Suter <francois@typo3.org>
Tested-by: Francois Suter <francois@typo3.org>
ChangeLog
Classes/Domain/Model/ExtensionConfiguration.php
Classes/Utility/Logger.php
Resources/Private/Language/Configuration.xlf
Tests/Unit/Domain/Model/ExtensionConfigurationTest.php
Tests/Unit/Utility/LoggerTest.php
ext_conf_template.txt

index 6078b66..997ffe2 100755 (executable)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,7 @@
 2016-10-30 Francois Suter <typo3@cobweb.ch>
 
        * Restored and improved log clearing feature, resolves #76972
+       * Added configuration option to restrict logging to certain keys, resolves #3053
 
 2016-09-04 Francois Suter <typo3@cobweb.ch>
 
index 92a13e9..d263028 100644 (file)
@@ -48,6 +48,13 @@ class ExtensionConfiguration implements SingletonInterface
     protected $excludeKeys = '';
 
     /**
+     * Include Keys
+     *
+     * @var string
+     */
+    protected $includeKeys = '';
+
+    /**
      * Ip Filter
      *
      * @var string
@@ -169,6 +176,27 @@ class ExtensionConfiguration implements SingletonInterface
     }
 
     /**
+     * Returns the includeKeys.
+     *
+     * @return string $includeKeys
+     */
+    public function getIncludeKeys()
+    {
+        return $this->includeKeys;
+    }
+
+    /**
+     * Sets the includeKeys.
+     *
+     * @param string $includeKeys
+     * @return void
+     */
+    public function setIncludeKeys($includeKeys)
+    {
+        $this->includeKeys = (string)$includeKeys;
+    }
+
+    /**
      * Returns the ipFilter.
      *
      * @return string $ipFilter
index f771ab8..ff60c48 100644 (file)
@@ -178,9 +178,17 @@ class Logger implements SingletonInterface
         if ($logData['severity'] < $this->extensionConfiguration->getMinimumLogLevel()) {
             return false;
         }
-        // Skip entry if key is in excluded list
-        if (GeneralUtility::inList($this->extensionConfiguration->getExcludeKeys(), $logData['extKey'])) {
-            return false;
+        // Check excluded list only if included list is empty
+        // (if included list is defined, it supersedes excluded list)
+        $includedList = $this->extensionConfiguration->getIncludeKeys();
+        if ($includedList === '') {
+            if (GeneralUtility::inList($this->extensionConfiguration->getExcludeKeys(), $logData['extKey'])) {
+                return false;
+            }
+        } else {
+            if (!GeneralUtility::inList($includedList, $logData['extKey'])) {
+                return false;
+            }
         }
         // Skip entry if referrer does not match IP mask
         if (!$this->isIpAddressAccepted($logData['ip'])) {
index a5841b4..ee8a78a 100644 (file)
@@ -27,6 +27,9 @@
                        <trans-unit id="excluded_keys">
                                <source>Excluded keys: Comma-separated list of (extension) keys whose entries should not be written to the logs.</source>
                        </trans-unit>
+                       <trans-unit id="included_keys">
+                               <source>Included keys: Comma-separated list of (extension) keys whose entries should be the only ones written to the logs (supersedes excluded keys, if also defined).</source>
+                       </trans-unit>
                        <trans-unit id="ip_filter">
                                <source>Restricted IP addresses: Only requests coming from the given IP addresses will be logged. An empty value will block everything, a "*" will allow everything. Use "devIPmask" (not case-sensitive) to reuse devIPmask value.</source>
                        </trans-unit>
index f5022ac..a410eed 100644 (file)
@@ -100,6 +100,30 @@ class ExtensionConfigurationTest extends UnitTestCase
     /**
      * @test
      */
+    public function getIncludeKeysInitiallyReturnsEmptryString()
+    {
+        self::assertSame(
+                '',
+                $this->subject->getIncludeKeys()
+        );
+    }
+
+    /**
+     * @test
+     */
+    public function setIncludeKeysSetsKeys()
+    {
+        $keys = 'core,extbase';
+        $this->subject->setIncludeKeys($keys);
+        self::assertSame(
+                $keys,
+                $this->subject->getIncludeKeys()
+        );
+    }
+
+    /**
+     * @test
+     */
     public function getIpFilterInitiallyReturnsEmptyString()
     {
         self::assertSame(
index de87f6b..367184d 100644 (file)
@@ -63,27 +63,54 @@ class LoggerTest extends UnitTestCase
         unset($this->extensionConfiguration, $this->subject);
     }
 
-    /**
-     * @test
-     * @covers \Devlog\Devlog\Utility\Logger::isEntryAccepted
-     */
-    public function entryIsAccepted()
+    public function validEntriesProvider()
     {
-        self::assertTrue(
-                $this->subject->isEntryAccepted(
+        return array(
+                'Valid entry without included keys list' => array(
+                        '',
                         array(
                                 'severity' => 2,
                                 'extKey' => 'whatever',
                                 'ip' => '127.0.0.1'
                         )
+                ),
+                'Valid entry with included keys list' => array(
+                        'foo',
+                        array(
+                                'severity' => 2,
+                                'extKey' => 'foo',
+                                'ip' => '127.0.0.1'
+                        )
                 )
         );
     }
 
-    public function validEntriesProvider()
+    /**
+     * @param string $includeKeys Comma-separated list of keys to include
+     * @param array $entry Entry data
+     * @test
+     * @dataProvider validEntriesProvider
+     * @covers \Devlog\Devlog\Utility\Logger::isEntryAccepted
+     */
+    public function entryIsAccepted($includeKeys, $entry)
+    {
+        $this->extensionConfiguration->setIncludeKeys($includeKeys);
+        $this->subject->setExtensionConfiguration($this->extensionConfiguration);
+        self::assertTrue(
+                $this->subject->isEntryAccepted($entry)
+        );
+    }
+
+    /**
+     * Provides a list of log entries that will be refused given the predefined configuration.
+     *
+     * @return array
+     */
+    public function invalidEntriesProvider()
     {
         return array(
                 'Severity too low' => array(
+                        '',
                         array(
                                 'severity' => 0,
                                 'extKey' => 'whatever',
@@ -91,13 +118,23 @@ class LoggerTest extends UnitTestCase
                         )
                 ),
                 'Excluded extension key' => array(
+                        '',
                         array(
                                 'severity' => 3,
                                 'extKey' => 'foo',
                                 'ip' => '127.0.0.1'
                         )
                 ),
+                'Not in included key list' => array(
+                        'baz',
+                        array(
+                                'severity' => 3,
+                                'extKey' => 'whatever',
+                                'ip' => '127.0.0.1'
+                        )
+                ),
                 'IP does not match' => array(
+                        '',
                         array(
                                 'severity' => 3,
                                 'extKey' => 'whatever',
@@ -108,13 +145,16 @@ class LoggerTest extends UnitTestCase
     }
 
     /**
+     * @param string $includeKeys Comma-separated list of keys to include
      * @param array $entry Log entry data
      * @test
-     * @dataProvider validEntriesProvider
+     * @dataProvider invalidEntriesProvider
      * @covers \Devlog\Devlog\Utility\Logger::isEntryAccepted
      */
-    public function entryIsRefused($entry)
+    public function entryIsRefused($includeKeys, $entry)
     {
+        $this->extensionConfiguration->setIncludeKeys($includeKeys);
+        $this->subject->setExtensionConfiguration($this->extensionConfiguration);
         self::assertFalse(
                 $this->subject->isEntryAccepted(
                         $entry
index 67dae62..f0fee06 100644 (file)
@@ -12,6 +12,9 @@ minimumLogLevel = -1
 # cat=general/filtering/b; type=string; label=LLL:EXT:devlog/Resources/Private/Language/Configuration.xlf:excluded_keys
 excludeKeys =
 
+# cat=general/filtering/b; type=string; label=LLL:EXT:devlog/Resources/Private/Language/Configuration.xlf:included_keys
+includeKeys =
+
 # cat=general/filtering/c; type=string; label=LLL:EXT:devlog/Resources/Private/Language/Configuration.xlf:ip_filter
 ipFilter =