Skip to content
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

Add a setupcheck for errors and warnings in log file #1021

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use OCA\LogReader\Listener\LogListener;
use OCA\LogReader\Log\Formatter;
use OCA\LogReader\SetupChecks\LogErrors;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
Expand All @@ -42,8 +43,10 @@
$context->registerService(Formatter::class, function (ContainerInterface $c) {
return new Formatter(\OC::$SERVERROOT);
});

$context->registerSetupCheck(LogErrors::class);
}

Check warning on line 49 in lib/AppInfo/Application.php

View check run for this annotation

Codecov / codecov/patch

lib/AppInfo/Application.php#L47-L49

Added lines #L47 - L49 were not covered by tests
public function boot(IBootContext $context): void {
}
}
96 changes: 96 additions & 0 deletions lib/SetupChecks/LogErrors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Côme Chilliet <come.chilliet@nextcloud.com>
*
* @author Côme Chilliet <come.chilliet@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\LogReader\SetupChecks;

use OCA\LogReader\Log\LogIteratorFactory;
use OCP\IConfig;
use OCP\IDateTimeFormatter;
use OCP\IL10N;
use OCP\SetupCheck\ISetupCheck;
use OCP\SetupCheck\SetupResult;

class LogErrors implements ISetupCheck {
private const LEVEL_WARNING = 2;
private const LEVEL_ERROR = 3;
private const LEVEL_FATAL = 4;

public function __construct(
private IL10N $l10n,
private IConfig $config,
private IDateTimeFormatter $dateFormatter,
private LogIteratorFactory $logIteratorFactory,
) {
}

public function getName(): string {
return $this->l10n->t('Errors in the log');
}

public function getCategory(): string {
return 'system';

Check warning on line 53 in lib/SetupChecks/LogErrors.php

View check run for this annotation

Codecov / codecov/patch

lib/SetupChecks/LogErrors.php#L52-L53

Added lines #L52 - L53 were not covered by tests
}

public function run(): SetupResult {
$logIterator = $this->logIteratorFactory->getLogIterator([self::LEVEL_WARNING,self::LEVEL_ERROR,self::LEVEL_FATAL]);
$count = [
self::LEVEL_WARNING => 0,
self::LEVEL_ERROR => 0,
self::LEVEL_FATAL => 0,
];
$limit = new \DateTime('7 days ago');
foreach ($logIterator as $logItem) {
if (!isset($logItem['time'])) {
break;

Check warning on line 66 in lib/SetupChecks/LogErrors.php

View check run for this annotation

Codecov / codecov/patch

lib/SetupChecks/LogErrors.php#L66

Added line #L66 was not covered by tests
come-nc marked this conversation as resolved.
Show resolved Hide resolved
}
$time = \DateTime::createFromFormat(\DateTime::ATOM, $logItem['time']);
if ($time < $limit) {
break;
}
$count[$logItem['level']]++;
}
if (array_sum($count) === 0) {
return SetupResult::success($this->l10n->t('No errors in the logs since %s', $this->dateFormatter->formatDate($limit)));
} elseif ($count[self::LEVEL_ERROR] + $count[self::LEVEL_FATAL] > 0) {
return SetupResult::error(
$this->l10n->n(
'%n error in the logs since %s',
'%n errors in the logs since %s',
$count[self::LEVEL_ERROR] + $count[self::LEVEL_FATAL],
[$this->dateFormatter->formatDate($limit)],
)
);
} else {
return SetupResult::warning(
$this->l10n->n(
'%n warning in the logs since %s',
'%n warnings in the logs since %s'.json_encode($count),
$count[self::LEVEL_WARNING],
[$this->dateFormatter->formatDate($limit)],
)
);
}
}
}
85 changes: 85 additions & 0 deletions tests/Unit/SetupChecks/LogErrorsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Côme Chilliet <come.chilliet@nextcloud.com>
*
* @author Côme Chilliet <come.chilliet@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\LogReader\Tests\Unit\SetupChecks;

use OCA\LogReader\Log\LogIteratorFactory;
use OCA\LogReader\SetupChecks\LogErrors;
use OCP\IConfig;
use OCP\IDateTimeFormatter;
use OCP\IL10N;
use Test\TestCase;

class LogErrorsTest extends TestCase {
private IL10N $l10n;
private IConfig $config;
private IDateTimeFormatter $dateFormatter;
private LogIteratorFactory $logIteratorFactory;
private LogErrors $logErrorsCheck;

protected function setUp(): void {
parent::setUp();

$this->l10n = $this->createMock(IL10N::class);
$this->config = $this->createMock(IConfig::class);
$this->dateFormatter = $this->createMock(IDateTimeFormatter::class);
$this->logIteratorFactory = $this->createMock(LogIteratorFactory::class);
$this->logErrorsCheck = new LogErrors(
$this->l10n,
$this->config,
$this->dateFormatter,
$this->logIteratorFactory,
);
}

public function logProvider(): array {
$now = (new \DateTime())->format(\DateTime::ATOM);
$tooOld = (new \DateTime('1 month ago'))->format(\DateTime::ATOM);
return [
[[], 'success'],
[[['level' => 2, 'time' => $now]], 'warning'],
[[['level' => 3, 'time' => $now]], 'error'],
[[['level' => 2, 'time' => $now],['level' => 3, 'time' => $now]], 'error'],
[[['level' => 2, 'time' => $now],['level' => 3, 'time' => $tooOld]], 'warning'],
[[['level' => 2, 'time' => $tooOld],['level' => 3, 'time' => $tooOld]], 'success'],
];
}

/**
* @dataProvider logProvider
*/
public function testSetupCheck(array $logContent, string $severity): void {
$logIterator = new \ArrayIterator($logContent);
$this->logIteratorFactory
->expects(self::once())
->method('getLogIterator')
->willReturn($logIterator);

$result = $this->logErrorsCheck->run();

$this->assertEquals($severity, $result->getSeverity());
}
}
Loading