-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(setupcheck): check logging level for validity #50927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\Settings\SetupChecks; | ||
|
|
||
| use OCP\IConfig; | ||
| use OCP\IL10N; | ||
| use OCP\ILogger; | ||
| use OCP\IURLGenerator; | ||
| use OCP\SetupCheck\ISetupCheck; | ||
| use OCP\SetupCheck\SetupResult; | ||
|
|
||
| class LoggingLevel implements ISetupCheck { | ||
| public function __construct( | ||
| private IL10N $l10n, | ||
| private IConfig $config, | ||
| private IURLGenerator $urlGenerator, | ||
| ) { | ||
| } | ||
|
|
||
| public function getName(): string { | ||
| return $this->l10n->t('Logging level'); | ||
| } | ||
|
|
||
| public function getCategory(): string { | ||
| return 'system'; | ||
| } | ||
|
|
||
| public function run(): SetupResult { | ||
| $configLogLevel = $this->config->getSystemValue('loglevel', ILogger::WARN); | ||
| if (!is_int($configLogLevel) | ||
| || $configLogLevel < ILogger::DEBUG | ||
| || $configLogLevel > ILogger::FATAL | ||
| ) { | ||
| return SetupResult::error( | ||
| $this->l10n->t('The %1$s configuration option must be a valid integer value.', ['`loglevel`']), | ||
| $this->urlGenerator->linkToDocs('admin-logging'), | ||
| ); | ||
| } | ||
|
|
||
| if ($configLogLevel === ILogger::DEBUG) { | ||
| return SetupResult::warning( | ||
| $this->l10n->t('The logging level is set to debug level. Use debug level only when you have a problem to diagnose, and then reset your log level to a less-verbose level as it outputs a lot of information, and can affect your server performance.'), | ||
| $this->urlGenerator->linkToDocs('admin-logging'), | ||
| ); | ||
| } | ||
|
|
||
| return SetupResult::success($this->l10n->t('Logging level configured correctly.')); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\Settings\Tests; | ||
|
|
||
| use OCA\Settings\SetupChecks\LoggingLevel; | ||
| use OCP\IConfig; | ||
| use OCP\IL10N; | ||
| use OCP\ILogger; | ||
| use OCP\IURLGenerator; | ||
| use OCP\SetupCheck\SetupResult; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use Psr\Log\LogLevel; | ||
| use Test\TestCase; | ||
|
|
||
| class LoggingLevelTest extends TestCase { | ||
| private IL10N&MockObject $l10n; | ||
| private IConfig&MockObject $config; | ||
| private IURLGenerator&MockObject $urlGenerator; | ||
|
|
||
| protected function setUp(): void { | ||
| parent::setUp(); | ||
|
|
||
| $this->l10n = $this->createMock(IL10N::class); | ||
| $this->l10n->expects($this->any()) | ||
| ->method('t') | ||
| ->willReturnCallback(function ($message, array $replace) { | ||
| return vsprintf($message, $replace); | ||
| }); | ||
| $this->config = $this->createMock(IConfig::class); | ||
| $this->urlGenerator = $this->createMock(IURLGenerator::class); | ||
| } | ||
|
|
||
| public static function dataRun(): array { | ||
| return [ | ||
| [ILogger::INFO, SetupResult::SUCCESS], | ||
| [ILogger::WARN, SetupResult::SUCCESS], | ||
| [ILogger::ERROR, SetupResult::SUCCESS], | ||
| [ILogger::FATAL, SetupResult::SUCCESS], | ||
|
|
||
| // Debug is valid but will result in an warning | ||
| [ILogger::DEBUG, SetupResult::WARNING], | ||
|
|
||
| // negative - invalid range | ||
| [-1, SetupResult::ERROR], | ||
| // string value instead of number | ||
| ['1', SetupResult::ERROR], | ||
| // random string value | ||
| ['error', SetupResult::ERROR], | ||
| // PSR logger value | ||
| [LogLevel::ALERT, SetupResult::ERROR], | ||
| // out of range | ||
| [ILogger::FATAL + 1, SetupResult::ERROR], | ||
| ]; | ||
| } | ||
|
|
||
| /** @dataProvider dataRun */ | ||
| public function testRun(mixed $value, string $expected): void { | ||
| $this->urlGenerator->method('linkToDocs')->willReturn('admin-logging'); | ||
|
|
||
| $this->config->expects(self::once()) | ||
| ->method('getSystemValue') | ||
| ->with('loglevel', ILogger::WARN) | ||
| ->willReturn($value); | ||
|
|
||
| $check = new LoggingLevel($this->l10n, $this->config, $this->urlGenerator); | ||
|
|
||
| $result = $check->run(); | ||
| $this->assertEquals($expected, $result->getSeverity()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.