Skip to content

Commit 00af121

Browse files
authored
Merge pull request #58601 from nextcloud/fix/noid/default-loglevel-with-match-condition
fix: Use configured loglevel even when log.condition matches is set
2 parents 694e231 + a56607e commit 00af121

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

lib/private/Log.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -269,20 +269,9 @@ public function getLogLevel(array $context, string $message): int {
269269
}
270270
}
271271

272-
if (!isset($logCondition['matches'])) {
273-
$configLogLevel = $this->config->getValue('loglevel', ILogger::WARN);
274-
if (is_numeric($configLogLevel)) {
275-
$this->nestingLevel--;
276-
return min((int)$configLogLevel, ILogger::FATAL);
277-
}
272+
$logConditionMatches = $logCondition['matches'] ?? [];
278273

279-
// Invalid configuration, warn the user and fall back to default level of WARN
280-
error_log('Nextcloud configuration: "loglevel" is not a valid integer');
281-
$this->nestingLevel--;
282-
return ILogger::WARN;
283-
}
284-
285-
foreach ($logCondition['matches'] as $option) {
274+
foreach ($logConditionMatches as $option) {
286275
if (
287276
(!isset($option['shared_secret']) || $this->checkLogSecret($option['shared_secret']))
288277
&& (!isset($option['users']) || in_array($userId, $option['users'], true))
@@ -300,6 +289,14 @@ public function getLogLevel(array $context, string $message): int {
300289
}
301290
}
302291

292+
$configLogLevel = $this->config->getValue('loglevel', ILogger::WARN);
293+
if (is_numeric($configLogLevel)) {
294+
$this->nestingLevel--;
295+
return min((int)$configLogLevel, ILogger::FATAL);
296+
}
297+
298+
// Invalid configuration, warn the user and fall back to default level of WARN
299+
error_log('Nextcloud configuration: "loglevel" is not a valid integer');
303300
$this->nestingLevel--;
304301
return ILogger::WARN;
305302
}

tests/lib/LoggerTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,34 @@ public function testMatchesCondition(string $userId, array $conditions, array $e
159159
$this->assertEquals($expectedLogs, $this->getLogs());
160160
}
161161

162+
public function testMatchesConditionIncreaseLoglevel(): void {
163+
$this->config->expects($this->any())
164+
->method('getValue')
165+
->willReturnMap([
166+
['loglevel', ILogger::WARN, ILogger::INFO],
167+
['log.condition', [], ['matches' => [
168+
[
169+
'message' => 'catched',
170+
'loglevel' => 3,
171+
]
172+
]]],
173+
]);
174+
$logger = $this->logger;
175+
176+
$user = $this->createMock(IUser::class);
177+
$user->method('getUID')
178+
->willReturn('test-userid');
179+
$userSession = $this->createMock(IUserSession::class);
180+
$userSession->method('getUser')
181+
->willReturn($user);
182+
$this->overwriteService(IUserSession::class, $userSession);
183+
184+
$logger->info('catched message');
185+
$logger->info('info level message');
186+
187+
$this->assertEquals(['1 info level message'], $this->getLogs());
188+
}
189+
162190
public function testLoggingWithDataArray(): void {
163191
$this->mockDefaultLogLevel();
164192
/** @var IWriter&MockObject */

0 commit comments

Comments
 (0)