|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright Copyright (c) 2023 Côme Chilliet <[email protected]> |
| 7 | + * |
| 8 | + * @author Côme Chilliet <[email protected]> |
| 9 | + * |
| 10 | + * @license GNU AGPL version 3 or any later version |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License as |
| 14 | + * published by the Free Software Foundation, either version 3 of the |
| 15 | + * License, or (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU Affero General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU Affero General Public License |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + * |
| 25 | + */ |
| 26 | +namespace OCA\LogReader\SetupChecks; |
| 27 | + |
| 28 | +use OCA\LogReader\Log\LogIteratorFactory; |
| 29 | +use OCP\IConfig; |
| 30 | +use OCP\IDateTimeFormatter; |
| 31 | +use OCP\IL10N; |
| 32 | +use OCP\SetupCheck\ISetupCheck; |
| 33 | +use OCP\SetupCheck\SetupResult; |
| 34 | + |
| 35 | +class LogErrors implements ISetupCheck { |
| 36 | + private const LEVEL_WARNING = 2; |
| 37 | + private const LEVEL_ERROR = 3; |
| 38 | + private const LEVEL_FATAL = 4; |
| 39 | + |
| 40 | + public function __construct( |
| 41 | + private IL10N $l10n, |
| 42 | + private IConfig $config, |
| 43 | + private IDateTimeFormatter $dateFormatter, |
| 44 | + private LogIteratorFactory $logIteratorFactory, |
| 45 | + ) { |
| 46 | + } |
| 47 | + |
| 48 | + public function getName(): string { |
| 49 | + return $this->l10n->t('Errors in the log'); |
| 50 | + } |
| 51 | + |
| 52 | + public function getCategory(): string { |
| 53 | + return 'system'; |
| 54 | + } |
| 55 | + |
| 56 | + public function run(): SetupResult { |
| 57 | + $logIterator = $this->logIteratorFactory->getLogIterator([self::LEVEL_WARNING,self::LEVEL_ERROR,self::LEVEL_FATAL]); |
| 58 | + $count = [ |
| 59 | + self::LEVEL_WARNING => 0, |
| 60 | + self::LEVEL_ERROR => 0, |
| 61 | + self::LEVEL_FATAL => 0, |
| 62 | + ]; |
| 63 | + $limit = new \DateTime('7 days ago'); |
| 64 | + foreach ($logIterator as $logItem) { |
| 65 | + if (!isset($logItem['time'])) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + $time = \DateTime::createFromFormat(\DateTime::ATOM, $logItem['time']); |
| 69 | + if ($time < $limit) { |
| 70 | + break; |
| 71 | + } |
| 72 | + $count[$logItem['level']]++; |
| 73 | + } |
| 74 | + if (array_sum($count) === 0) { |
| 75 | + return SetupResult::success($this->l10n->t('No errors in the logs since %s', $this->dateFormatter->formatDate($limit))); |
| 76 | + } elseif ($count[self::LEVEL_ERROR] + $count[self::LEVEL_FATAL] > 0) { |
| 77 | + return SetupResult::error( |
| 78 | + $this->l10n->n( |
| 79 | + '%n error in the logs since %s', |
| 80 | + '%n errors in the logs since %s', |
| 81 | + $count[self::LEVEL_ERROR] + $count[self::LEVEL_FATAL], |
| 82 | + [$this->dateFormatter->formatDate($limit)], |
| 83 | + ) |
| 84 | + ); |
| 85 | + } else { |
| 86 | + return SetupResult::warning( |
| 87 | + $this->l10n->n( |
| 88 | + '%n warning in the logs since %s', |
| 89 | + '%n warnings in the logs since %s'.json_encode($count), |
| 90 | + $count[self::LEVEL_WARNING], |
| 91 | + [$this->dateFormatter->formatDate($limit)], |
| 92 | + ) |
| 93 | + ); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments