Skip to content

Commit bca9075

Browse files
authored
Merge pull request #58613 from nextcloud/backport/58601/stable32
[stable32] fix: Use configured loglevel even when log.condition matches is set
2 parents 79bc67f + 0082494 commit bca9075

2 files changed

Lines changed: 38 additions & 13 deletions

File tree

lib/private/Log.php

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

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

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

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

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)