|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * @copyright Copyright (c) 2023 Robin Appelman <[email protected]> |
| 6 | + * |
| 7 | + * @license GNU AGPL version 3 or any later version |
| 8 | + * |
| 9 | + * This program is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Affero General Public License as |
| 11 | + * published by the Free Software Foundation, either version 3 of the |
| 12 | + * License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Affero General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Affero General Public License |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +namespace OCA\LogReader\Log; |
| 25 | + |
| 26 | +use OC\Log\LogDetails; |
| 27 | +use OC\SystemConfig; |
| 28 | +use OCA\LogReader\Command\Tail; |
| 29 | + |
| 30 | +/** |
| 31 | + * Utility to write log messages to the console as they are emitted |
| 32 | + */ |
| 33 | +class Console extends LogDetails { |
| 34 | + private int $level; |
| 35 | + private int $terminalWidth; |
| 36 | + private Formatter $formatter; |
| 37 | + |
| 38 | + public function __construct(Formatter $formatter, SystemConfig $config, string $level, int $terminalWidth) { |
| 39 | + parent::__construct($config); |
| 40 | + $this->formatter = $formatter; |
| 41 | + $this->level = self::parseLogLevel($level); |
| 42 | + $this->terminalWidth = $terminalWidth; |
| 43 | + } |
| 44 | + |
| 45 | + public function log(int $level, string $app, array $entry) { |
| 46 | + if ($level >= $this->level) { |
| 47 | + $messageWidth = $this->terminalWidth - 8 - 18 - 6; |
| 48 | + |
| 49 | + $entry = $this->logDetails($app, $entry, $level); |
| 50 | + |
| 51 | + $lines = explode("\n", $this->formatter->formatMessage($entry, $messageWidth)); |
| 52 | + $lines[0] = str_pad(Tail::LEVELS[$level], 8) . ' ' . |
| 53 | + str_pad(wordwrap($app, 18), 18) . ' ' . |
| 54 | + str_pad($lines[0], $messageWidth); |
| 55 | + |
| 56 | + for ($i = 1; $i < count($lines); $i++) { |
| 57 | + $lines[$i] = str_repeat(' ', 8 + 18 + 2) . $lines[$i]; |
| 58 | + } |
| 59 | + |
| 60 | + foreach ($lines as $line) { |
| 61 | + fwrite(STDERR, $line . "\n"); |
| 62 | + } |
| 63 | + fwrite(STDERR, "\n"); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private static function parseLogLevel(string $level): int { |
| 68 | + if (is_numeric($level)) { |
| 69 | + return (int)$level; |
| 70 | + } |
| 71 | + |
| 72 | + switch (strtoupper($level)) { |
| 73 | + case "DEBUG": |
| 74 | + return 0; |
| 75 | + case "INFO": |
| 76 | + return 1; |
| 77 | + case "WARN": |
| 78 | + return 2; |
| 79 | + case "ERROR": |
| 80 | + return 3; |
| 81 | + case "FATAL": |
| 82 | + return 4; |
| 83 | + default: |
| 84 | + throw new \Exception("Unknown log level $level"); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments