|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * @copyright 2022 Christoph Wurst <[email protected]> |
| 7 | + * |
| 8 | + * @author 2022 Christoph Wurst <[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 OCP\Log; |
| 27 | + |
| 28 | +use OC; |
| 29 | +use OCP\AppFramework\QueryException; |
| 30 | +use Psr\Log\LoggerInterface; |
| 31 | +use Psr\Log\NullLogger; |
| 32 | +use function class_exists; |
| 33 | + |
| 34 | +/** |
| 35 | + * Get a PSR logger |
| 36 | + * |
| 37 | + * Whenever possible, inject a logger into your classes instead of relying on |
| 38 | + * this helper function. |
| 39 | + * |
| 40 | + * @warning the returned logger implementation is not guaranteed to be the same |
| 41 | + * between two function calls. During early stages of the process you |
| 42 | + * might in fact get a noop implementation when Nextcloud isn't ready |
| 43 | + * to log. Therefore you MUST NOT cache the result of this function but |
| 44 | + * fetch a new logger for every log line you want to write. |
| 45 | + * |
| 46 | + * @param string|null $appId optional parameter to acquire the app-specific logger |
| 47 | + * |
| 48 | + * @return LoggerInterface |
| 49 | + * @since 24.0.0 |
| 50 | + */ |
| 51 | +function logger(string $appId = null): LoggerInterface { |
| 52 | + if (!class_exists(OC::class) || OC::$server === null) { |
| 53 | + // If someone calls this log before Nextcloud is initialized, there is |
| 54 | + // no logging available. In that case we return a noop implementation |
| 55 | + // TODO: evaluate whether logging to error_log could be an alternative |
| 56 | + return new NullLogger(); |
| 57 | + } |
| 58 | + |
| 59 | + if ($appId !== null) { |
| 60 | + try { |
| 61 | + $appContainer = OC::$server->getRegisteredAppContainer($appId); |
| 62 | + return $appContainer->get(LoggerInterface::class); |
| 63 | + } catch (QueryException $e) { |
| 64 | + // Ignore and return the server logger below |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + return OC::$server->get(LoggerInterface::class); |
| 70 | + } catch (QueryException $e) { |
| 71 | + return new NullLogger(); |
| 72 | + } |
| 73 | +} |
0 commit comments