diff options
Diffstat (limited to 'lib/public/Log')
-rw-r--r-- | lib/public/Log/Audit/CriticalActionPerformedEvent.php | 66 | ||||
-rw-r--r-- | lib/public/Log/BeforeMessageLoggedEvent.php | 66 | ||||
-rw-r--r-- | lib/public/Log/IDataLogger.php | 23 | ||||
-rw-r--r-- | lib/public/Log/IFileBased.php | 24 | ||||
-rw-r--r-- | lib/public/Log/ILogFactory.php | 34 | ||||
-rw-r--r-- | lib/public/Log/IWriter.php | 27 | ||||
-rw-r--r-- | lib/public/Log/RotationTrait.php | 28 | ||||
-rw-r--r-- | lib/public/Log/functions.php | 58 |
8 files changed, 216 insertions, 110 deletions
diff --git a/lib/public/Log/Audit/CriticalActionPerformedEvent.php b/lib/public/Log/Audit/CriticalActionPerformedEvent.php new file mode 100644 index 00000000000..da7bdda8c73 --- /dev/null +++ b/lib/public/Log/Audit/CriticalActionPerformedEvent.php @@ -0,0 +1,66 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OCP\Log\Audit; + +use OCP\EventDispatcher\Event; + +/** + * Emitted when the admin_audit app should log an entry + * + * @since 22.0.0 + */ +class CriticalActionPerformedEvent extends Event { + /** @var string */ + private $logMessage; + + /** @var array */ + private $parameters; + + /** @var bool */ + private $obfuscateParameters; + + /** + * @param string $logMessage + * @param array $parameters + * @param bool $obfuscateParameters + * @since 22.0.0 + */ + public function __construct(string $logMessage, + array $parameters = [], + bool $obfuscateParameters = false) { + parent::__construct(); + $this->logMessage = $logMessage; + $this->parameters = $parameters; + $this->obfuscateParameters = $obfuscateParameters; + } + + /** + * @return string + * @since 22.0.0 + */ + public function getLogMessage(): string { + return $this->logMessage; + } + + /** + * @return array + * @since 22.0.0 + */ + public function getParameters(): array { + return $this->parameters; + } + + /** + * @return bool + * @since 22.0.0 + */ + public function getObfuscateParameters(): bool { + return $this->obfuscateParameters; + } +} diff --git a/lib/public/Log/BeforeMessageLoggedEvent.php b/lib/public/Log/BeforeMessageLoggedEvent.php new file mode 100644 index 00000000000..8500a769bca --- /dev/null +++ b/lib/public/Log/BeforeMessageLoggedEvent.php @@ -0,0 +1,66 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCP\Log; + +use OCP\EventDispatcher\Event; + +/** + * Even for when a log item is being logged + * + * @since 28.0.0 + */ +class BeforeMessageLoggedEvent extends Event { + private int $level; + private string $app; + private $message; + + /** + * @param string $app + * @param int $level + * @param array $message + * @since 28.0.0 + */ + public function __construct(string $app, int $level, array $message) { + $this->level = $level; + $this->app = $app; + $this->message = $message; + } + + /** + * Get the level of the log item + * + * @return int + * @since 28.0.0 + */ + public function getLevel(): int { + return $this->level; + } + + + /** + * Get the app context of the log item + * + * @return string + * @since 28.0.0 + */ + public function getApp(): string { + return $this->app; + } + + + /** + * Get the message of the log item + * + * @return array + * @since 28.0.0 + */ + public function getMessage(): array { + return $this->message; + } +} diff --git a/lib/public/Log/IDataLogger.php b/lib/public/Log/IDataLogger.php index ed56009ca05..c3334d0bf12 100644 --- a/lib/public/Log/IDataLogger.php +++ b/lib/public/Log/IDataLogger.php @@ -3,27 +3,9 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2019 Arthur Schiwon <blizzz@arthur-schiwon.de> - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCP\Log; /** @@ -32,7 +14,6 @@ namespace OCP\Log; * @since 18.0.1 */ interface IDataLogger { - /** * allows to log custom data, similar to how logException works * diff --git a/lib/public/Log/IFileBased.php b/lib/public/Log/IFileBased.php index eeebb766cda..ace10ee1b68 100644 --- a/lib/public/Log/IFileBased.php +++ b/lib/public/Log/IFileBased.php @@ -1,27 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de> - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCP\Log; /** diff --git a/lib/public/Log/ILogFactory.php b/lib/public/Log/ILogFactory.php index a2cb02e8759..db7adca2192 100644 --- a/lib/public/Log/ILogFactory.php +++ b/lib/public/Log/ILogFactory.php @@ -1,30 +1,12 @@ <?php + /** - * @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de> - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Johannes Ernst <jernst@indiecomputing.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/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCP\Log; -use OCP\ILogger; +use Psr\Log\LoggerInterface; /** * Interface ILogFactory @@ -41,8 +23,10 @@ interface ILogFactory { /** * @param string $path - * @return ILogger - * @since 14.0.0 + * @param string $type + * @param string $tag + * @return LoggerInterface + * @since 22.0.0 - Parameters $type and $tag were added in 24.0.0 */ - public function getCustomLogger(string $path): ILogger; + public function getCustomPsrLogger(string $path, string $type = 'file', string $tag = 'Nextcloud'): LoggerInterface; } diff --git a/lib/public/Log/IWriter.php b/lib/public/Log/IWriter.php index bc8feb908b9..2fcbc094881 100644 --- a/lib/public/Log/IWriter.php +++ b/lib/public/Log/IWriter.php @@ -1,26 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de> - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCP\Log; /** @@ -31,6 +14,10 @@ namespace OCP\Log; interface IWriter { /** * @since 14.0.0 + * + * @param string $app + * @param string|array $message + * @param int $level */ public function write(string $app, $message, int $level); } diff --git a/lib/public/Log/RotationTrait.php b/lib/public/Log/RotationTrait.php index d7de50410d6..73b3b16b665 100644 --- a/lib/public/Log/RotationTrait.php +++ b/lib/public/Log/RotationTrait.php @@ -1,26 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de> - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCP\Log; /** @@ -30,7 +13,6 @@ namespace OCP\Log; * @since 14.0.0 */ trait RotationTrait { - /** * @var string * @since 14.0.0 @@ -48,7 +30,7 @@ trait RotationTrait { * @since 14.0.0 */ protected function rotate():string { - $rotatedFile = $this->filePath.'.1'; + $rotatedFile = $this->filePath . '.1'; rename($this->filePath, $rotatedFile); return $rotatedFile; } @@ -58,7 +40,7 @@ trait RotationTrait { * @since 14.0.0 */ protected function shouldRotateBySize():bool { - if ((int)$this->maxSize > 0) { + if ((int)$this->maxSize > 0 && file_exists($this->filePath)) { $filesize = @filesize($this->filePath); if ($filesize >= (int)$this->maxSize) { return true; diff --git a/lib/public/Log/functions.php b/lib/public/Log/functions.php new file mode 100644 index 00000000000..75e07deab34 --- /dev/null +++ b/lib/public/Log/functions.php @@ -0,0 +1,58 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCP\Log; + +use OC; +use OCP\AppFramework\QueryException; +use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; +use function class_exists; + +/** + * Get a PSR logger + * + * Whenever possible, inject a logger into your classes instead of relying on + * this helper function. + * + * @warning the returned logger implementation is not guaranteed to be the same + * between two function calls. During early stages of the process you + * might in fact get a noop implementation when Nextcloud isn't ready + * to log. Therefore you MUST NOT cache the result of this function but + * fetch a new logger for every log line you want to write. + * + * @param string|null $appId optional parameter to acquire the app-specific logger + * + * @return LoggerInterface + * @since 24.0.0 + */ +function logger(?string $appId = null): LoggerInterface { + /** @psalm-suppress TypeDoesNotContainNull false-positive, it may contain null if we are logging from initialization */ + if (!class_exists(OC::class) || OC::$server === null) { + // If someone calls this log before Nextcloud is initialized, there is + // no logging available. In that case we return a noop implementation + // TODO: evaluate whether logging to error_log could be an alternative + return new NullLogger(); + } + + if ($appId !== null) { + try { + $appContainer = OC::$server->getRegisteredAppContainer($appId); + return $appContainer->get(LoggerInterface::class); + } catch (QueryException $e) { + // Ignore and return the server logger below + } + } + + try { + return OC::$server->get(LoggerInterface::class); + } catch (QueryException $e) { + return new NullLogger(); + } +} |