|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> |
| 7 | + * |
| 8 | + * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at> |
| 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 OC\Log; |
| 27 | + |
| 28 | +use OCP\ILogger; |
| 29 | +use Psr\Log\InvalidArgumentException; |
| 30 | +use Psr\Log\LoggerInterface; |
| 31 | + |
| 32 | +final class PsrLoggerAdapter implements LoggerInterface { |
| 33 | + |
| 34 | + /** @var ILogger */ |
| 35 | + private $logger; |
| 36 | + |
| 37 | + public function __construct(ILogger $logger) { |
| 38 | + $this->logger = $logger; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * System is unusable. |
| 43 | + * |
| 44 | + * @param string $message |
| 45 | + * @param array $context |
| 46 | + * |
| 47 | + * @return void |
| 48 | + */ |
| 49 | + public function emergency($message, array $context = []): void { |
| 50 | + $this->logger->emergency($message, $context); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Action must be taken immediately. |
| 55 | + * |
| 56 | + * Example: Entire website down, database unavailable, etc. This should |
| 57 | + * trigger the SMS alerts and wake you up. |
| 58 | + * |
| 59 | + * @param string $message |
| 60 | + * @param array $context |
| 61 | + * |
| 62 | + * @return void |
| 63 | + */ |
| 64 | + public function alert($message, array $context = []) { |
| 65 | + $this->logger->alert($message, $context); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Critical conditions. |
| 70 | + * |
| 71 | + * Example: Application component unavailable, unexpected exception. |
| 72 | + * |
| 73 | + * @param string $message |
| 74 | + * @param array $context |
| 75 | + * |
| 76 | + * @return void |
| 77 | + */ |
| 78 | + public function critical($message, array $context = []) { |
| 79 | + $this->logger->critical($message, $context); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Runtime errors that do not require immediate action but should typically |
| 84 | + * be logged and monitored. |
| 85 | + * |
| 86 | + * @param string $message |
| 87 | + * @param array $context |
| 88 | + * |
| 89 | + * @return void |
| 90 | + */ |
| 91 | + public function error($message, array $context = []) { |
| 92 | + $this->logger->error($message, $context); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Exceptional occurrences that are not errors. |
| 97 | + * |
| 98 | + * Example: Use of deprecated APIs, poor use of an API, undesirable things |
| 99 | + * that are not necessarily wrong. |
| 100 | + * |
| 101 | + * @param string $message |
| 102 | + * @param array $context |
| 103 | + * |
| 104 | + * @return void |
| 105 | + */ |
| 106 | + public function warning($message, array $context = []) { |
| 107 | + $this->logger->warning($message, $context); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Normal but significant events. |
| 112 | + * |
| 113 | + * @param string $message |
| 114 | + * @param array $context |
| 115 | + * |
| 116 | + * @return void |
| 117 | + */ |
| 118 | + public function notice($message, array $context = []) { |
| 119 | + $this->logger->notice($message, $context); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Interesting events. |
| 124 | + * |
| 125 | + * Example: User logs in, SQL logs. |
| 126 | + * |
| 127 | + * @param string $message |
| 128 | + * @param array $context |
| 129 | + * |
| 130 | + * @return void |
| 131 | + */ |
| 132 | + public function info($message, array $context = []) { |
| 133 | + $this->logger->info($message, $context); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Detailed debug information. |
| 138 | + * |
| 139 | + * @param string $message |
| 140 | + * @param array $context |
| 141 | + * |
| 142 | + * @return void |
| 143 | + */ |
| 144 | + public function debug($message, array $context = []) { |
| 145 | + $this->logger->debug($message, $context); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Logs with an arbitrary level. |
| 150 | + * |
| 151 | + * @param mixed $level |
| 152 | + * @param string $message |
| 153 | + * @param array $context |
| 154 | + * |
| 155 | + * @return void |
| 156 | + * |
| 157 | + * @throws InvalidArgumentException |
| 158 | + */ |
| 159 | + public function log($level, $message, array $context = []) { |
| 160 | + if (!is_int($level) || $level < ILogger::DEBUG || $level > ILogger::FATAL) { |
| 161 | + throw new InvalidArgumentException('Nextcloud allows only integer log levels'); |
| 162 | + } |
| 163 | + $this->logger->log($level, $message, $context); |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments