Skip to content

Commit

Permalink
Add a setupcheck for errors and warnings in log file
Browse files Browse the repository at this point in the history
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Nov 7, 2023
1 parent df9ce9f commit cf6de7f
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
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,6 +43,8 @@ public function register(IRegistrationContext $context): void {
$context->registerService(Formatter::class, function (ContainerInterface $c) {
return new Formatter(\OC::$SERVERROOT);
});

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

public function boot(IBootContext $context): void {
Expand Down
98 changes: 98 additions & 0 deletions lib/SetupChecks/LogErrors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?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';
}

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;
}
$time = \DateTime::createFromFormat(\DateTime::ATOM, $logItem['time']);
if ($time < $limit) {
break;
}
$count[$logItem['level']]++;
}
if (array_sum($count) === 0) {
return new SetupResult(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 new SetupResult(
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 new SetupResult(
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)],
)
);
}
}
}

0 comments on commit cf6de7f

Please sign in to comment.