Skip to content

Commit 8d4746e

Browse files
author
Francois Suter
committed
[FEATURE] Add include keys list
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 <[email protected]> Tested-by: Francois Suter <[email protected]>
1 parent 7edc33b commit 8d4746e

File tree

7 files changed

+120
-13
lines changed

7 files changed

+120
-13
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
2016-10-30 Francois Suter <[email protected]>
22

33
* Restored and improved log clearing feature, resolves #76972
4+
* Added configuration option to restrict logging to certain keys, resolves #3053
45

56
2016-09-04 Francois Suter <[email protected]>
67

Classes/Domain/Model/ExtensionConfiguration.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ class ExtensionConfiguration implements SingletonInterface
4747
*/
4848
protected $excludeKeys = '';
4949

50+
/**
51+
* Include Keys
52+
*
53+
* @var string
54+
*/
55+
protected $includeKeys = '';
56+
5057
/**
5158
* Ip Filter
5259
*
@@ -168,6 +175,27 @@ public function setExcludeKeys($excludeKeys)
168175
$this->excludeKeys = (string)$excludeKeys;
169176
}
170177

178+
/**
179+
* Returns the includeKeys.
180+
*
181+
* @return string $includeKeys
182+
*/
183+
public function getIncludeKeys()
184+
{
185+
return $this->includeKeys;
186+
}
187+
188+
/**
189+
* Sets the includeKeys.
190+
*
191+
* @param string $includeKeys
192+
* @return void
193+
*/
194+
public function setIncludeKeys($includeKeys)
195+
{
196+
$this->includeKeys = (string)$includeKeys;
197+
}
198+
171199
/**
172200
* Returns the ipFilter.
173201
*

Classes/Utility/Logger.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,17 @@ public function isEntryAccepted($logData)
178178
if ($logData['severity'] < $this->extensionConfiguration->getMinimumLogLevel()) {
179179
return false;
180180
}
181-
// Skip entry if key is in excluded list
182-
if (GeneralUtility::inList($this->extensionConfiguration->getExcludeKeys(), $logData['extKey'])) {
183-
return false;
181+
// Check excluded list only if included list is empty
182+
// (if included list is defined, it supersedes excluded list)
183+
$includedList = $this->extensionConfiguration->getIncludeKeys();
184+
if ($includedList === '') {
185+
if (GeneralUtility::inList($this->extensionConfiguration->getExcludeKeys(), $logData['extKey'])) {
186+
return false;
187+
}
188+
} else {
189+
if (!GeneralUtility::inList($includedList, $logData['extKey'])) {
190+
return false;
191+
}
184192
}
185193
// Skip entry if referrer does not match IP mask
186194
if (!$this->isIpAddressAccepted($logData['ip'])) {

Resources/Private/Language/Configuration.xlf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
<trans-unit id="excluded_keys">
2828
<source>Excluded keys: Comma-separated list of (extension) keys whose entries should not be written to the logs.</source>
2929
</trans-unit>
30+
<trans-unit id="included_keys">
31+
<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>
32+
</trans-unit>
3033
<trans-unit id="ip_filter">
3134
<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>
3235
</trans-unit>

Tests/Unit/Domain/Model/ExtensionConfigurationTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,30 @@ public function setExcludeKeysSetsKeys()
9797
);
9898
}
9999

100+
/**
101+
* @test
102+
*/
103+
public function getIncludeKeysInitiallyReturnsEmptryString()
104+
{
105+
self::assertSame(
106+
'',
107+
$this->subject->getIncludeKeys()
108+
);
109+
}
110+
111+
/**
112+
* @test
113+
*/
114+
public function setIncludeKeysSetsKeys()
115+
{
116+
$keys = 'core,extbase';
117+
$this->subject->setIncludeKeys($keys);
118+
self::assertSame(
119+
$keys,
120+
$this->subject->getIncludeKeys()
121+
);
122+
}
123+
100124
/**
101125
* @test
102126
*/

Tests/Unit/Utility/LoggerTest.php

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,41 +63,78 @@ protected function tearDown()
6363
unset($this->extensionConfiguration, $this->subject);
6464
}
6565

66-
/**
67-
* @test
68-
* @covers \Devlog\Devlog\Utility\Logger::isEntryAccepted
69-
*/
70-
public function entryIsAccepted()
66+
public function validEntriesProvider()
7167
{
72-
self::assertTrue(
73-
$this->subject->isEntryAccepted(
68+
return array(
69+
'Valid entry without included keys list' => array(
70+
'',
7471
array(
7572
'severity' => 2,
7673
'extKey' => 'whatever',
7774
'ip' => '127.0.0.1'
7875
)
76+
),
77+
'Valid entry with included keys list' => array(
78+
'foo',
79+
array(
80+
'severity' => 2,
81+
'extKey' => 'foo',
82+
'ip' => '127.0.0.1'
83+
)
7984
)
8085
);
8186
}
8287

83-
public function validEntriesProvider()
88+
/**
89+
* @param string $includeKeys Comma-separated list of keys to include
90+
* @param array $entry Entry data
91+
* @test
92+
* @dataProvider validEntriesProvider
93+
* @covers \Devlog\Devlog\Utility\Logger::isEntryAccepted
94+
*/
95+
public function entryIsAccepted($includeKeys, $entry)
96+
{
97+
$this->extensionConfiguration->setIncludeKeys($includeKeys);
98+
$this->subject->setExtensionConfiguration($this->extensionConfiguration);
99+
self::assertTrue(
100+
$this->subject->isEntryAccepted($entry)
101+
);
102+
}
103+
104+
/**
105+
* Provides a list of log entries that will be refused given the predefined configuration.
106+
*
107+
* @return array
108+
*/
109+
public function invalidEntriesProvider()
84110
{
85111
return array(
86112
'Severity too low' => array(
113+
'',
87114
array(
88115
'severity' => 0,
89116
'extKey' => 'whatever',
90117
'ip' => '127.0.0.1'
91118
)
92119
),
93120
'Excluded extension key' => array(
121+
'',
94122
array(
95123
'severity' => 3,
96124
'extKey' => 'foo',
97125
'ip' => '127.0.0.1'
98126
)
99127
),
128+
'Not in included key list' => array(
129+
'baz',
130+
array(
131+
'severity' => 3,
132+
'extKey' => 'whatever',
133+
'ip' => '127.0.0.1'
134+
)
135+
),
100136
'IP does not match' => array(
137+
'',
101138
array(
102139
'severity' => 3,
103140
'extKey' => 'whatever',
@@ -108,13 +145,16 @@ public function validEntriesProvider()
108145
}
109146

110147
/**
148+
* @param string $includeKeys Comma-separated list of keys to include
111149
* @param array $entry Log entry data
112150
* @test
113-
* @dataProvider validEntriesProvider
151+
* @dataProvider invalidEntriesProvider
114152
* @covers \Devlog\Devlog\Utility\Logger::isEntryAccepted
115153
*/
116-
public function entryIsRefused($entry)
154+
public function entryIsRefused($includeKeys, $entry)
117155
{
156+
$this->extensionConfiguration->setIncludeKeys($includeKeys);
157+
$this->subject->setExtensionConfiguration($this->extensionConfiguration);
118158
self::assertFalse(
119159
$this->subject->isEntryAccepted(
120160
$entry

ext_conf_template.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ minimumLogLevel = -1
1212
# cat=general/filtering/b; type=string; label=LLL:EXT:devlog/Resources/Private/Language/Configuration.xlf:excluded_keys
1313
excludeKeys =
1414

15+
# cat=general/filtering/b; type=string; label=LLL:EXT:devlog/Resources/Private/Language/Configuration.xlf:included_keys
16+
includeKeys =
17+
1518
# cat=general/filtering/c; type=string; label=LLL:EXT:devlog/Resources/Private/Language/Configuration.xlf:ip_filter
1619
ipFilter =
1720

0 commit comments

Comments
 (0)