aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Log/PsrLoggerAdapter.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Log/PsrLoggerAdapter.php')
-rw-r--r--lib/private/Log/PsrLoggerAdapter.php224
1 files changed, 66 insertions, 158 deletions
diff --git a/lib/private/Log/PsrLoggerAdapter.php b/lib/private/Log/PsrLoggerAdapter.php
index 1cf81c49a69..79e9d4d9621 100644
--- a/lib/private/Log/PsrLoggerAdapter.php
+++ b/lib/private/Log/PsrLoggerAdapter.php
@@ -3,44 +3,44 @@
declare(strict_types=1);
/**
- * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
- *
- * @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: 2020 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Log;
use OC\Log;
+use OCP\EventDispatcher\IEventDispatcher;
use OCP\ILogger;
use OCP\Log\IDataLogger;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LoggerInterface;
+use Psr\Log\LogLevel;
use Throwable;
use function array_key_exists;
use function array_merge;
final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
+ public function __construct(
+ private Log $logger,
+ ) {
+ }
- /** @var Log */
- private $logger;
+ public static function logLevelToInt(string $level): int {
+ return match ($level) {
+ LogLevel::ALERT => ILogger::ERROR,
+ LogLevel::CRITICAL => ILogger::ERROR,
+ LogLevel::DEBUG => ILogger::DEBUG,
+ LogLevel::EMERGENCY => ILogger::FATAL,
+ LogLevel::ERROR => ILogger::ERROR,
+ LogLevel::INFO => ILogger::INFO,
+ LogLevel::NOTICE => ILogger::INFO,
+ LogLevel::WARNING => ILogger::WARN,
+ default => throw new InvalidArgumentException('Unsupported custom log level'),
+ };
+ }
- public function __construct(Log $logger) {
- $this->logger = $logger;
+ public function setEventDispatcher(IEventDispatcher $eventDispatcher): void {
+ $this->logger->setEventDispatcher($eventDispatcher);
}
private function containsThrowable(array $context): bool {
@@ -50,23 +50,11 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
/**
* System is unusable.
*
- * @param string $message
- * @param array $context
- *
- * @return void
+ * @param $message
+ * @param mixed[] $context
*/
public function emergency($message, array $context = []): void {
- if ($this->containsThrowable($context)) {
- $this->logger->logException($context['exception'], array_merge(
- [
- 'message' => $message,
- 'level' => ILogger::FATAL,
- ],
- $context
- ));
- } else {
- $this->logger->emergency($message, $context);
- }
+ $this->log(LogLevel::EMERGENCY, (string)$message, $context);
}
/**
@@ -75,23 +63,11 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
* Example: Entire website down, database unavailable, etc. This should
* trigger the SMS alerts and wake you up.
*
- * @param string $message
- * @param array $context
- *
- * @return void
+ * @param $message
+ * @param mixed[] $context
*/
- public function alert($message, array $context = []) {
- if ($this->containsThrowable($context)) {
- $this->logger->logException($context['exception'], array_merge(
- [
- 'message' => $message,
- 'level' => ILogger::ERROR,
- ],
- $context
- ));
- } else {
- $this->logger->alert($message, $context);
- }
+ public function alert($message, array $context = []): void {
+ $this->log(LogLevel::ALERT, (string)$message, $context);
}
/**
@@ -99,46 +75,22 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
*
* Example: Application component unavailable, unexpected exception.
*
- * @param string $message
- * @param array $context
- *
- * @return void
+ * @param $message
+ * @param mixed[] $context
*/
- public function critical($message, array $context = []) {
- if ($this->containsThrowable($context)) {
- $this->logger->logException($context['exception'], array_merge(
- [
- 'message' => $message,
- 'level' => ILogger::ERROR,
- ],
- $context
- ));
- } else {
- $this->logger->critical($message, $context);
- }
+ public function critical($message, array $context = []): void {
+ $this->log(LogLevel::CRITICAL, (string)$message, $context);
}
/**
* Runtime errors that do not require immediate action but should typically
* be logged and monitored.
*
- * @param string $message
- * @param array $context
- *
- * @return void
+ * @param $message
+ * @param mixed[] $context
*/
- public function error($message, array $context = []) {
- if ($this->containsThrowable($context)) {
- $this->logger->logException($context['exception'], array_merge(
- [
- 'message' => $message,
- 'level' => ILogger::ERROR,
- ],
- $context
- ));
- } else {
- $this->logger->error($message, $context);
- }
+ public function error($message, array $context = []): void {
+ $this->log(LogLevel::ERROR, (string)$message, $context);
}
/**
@@ -147,45 +99,21 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
* Example: Use of deprecated APIs, poor use of an API, undesirable things
* that are not necessarily wrong.
*
- * @param string $message
- * @param array $context
- *
- * @return void
+ * @param $message
+ * @param mixed[] $context
*/
- public function warning($message, array $context = []) {
- if ($this->containsThrowable($context)) {
- $this->logger->logException($context['exception'], array_merge(
- [
- 'message' => $message,
- 'level' => ILogger::WARN,
- ],
- $context
- ));
- } else {
- $this->logger->warning($message, $context);
- }
+ public function warning($message, array $context = []): void {
+ $this->log(LogLevel::WARNING, (string)$message, $context);
}
/**
* Normal but significant events.
*
- * @param string $message
- * @param array $context
- *
- * @return void
+ * @param $message
+ * @param mixed[] $context
*/
- public function notice($message, array $context = []) {
- if ($this->containsThrowable($context)) {
- $this->logger->logException($context['exception'], array_merge(
- [
- 'message' => $message,
- 'level' => ILogger::INFO,
- ],
- $context
- ));
- } else {
- $this->logger->notice($message, $context);
- }
+ public function notice($message, array $context = []): void {
+ $this->log(LogLevel::NOTICE, (string)$message, $context);
}
/**
@@ -193,72 +121,52 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
*
* Example: User logs in, SQL logs.
*
- * @param string $message
- * @param array $context
- *
- * @return void
+ * @param $message
+ * @param mixed[] $context
*/
- public function info($message, array $context = []) {
- if ($this->containsThrowable($context)) {
- $this->logger->logException($context['exception'], array_merge(
- [
- 'message' => $message,
- 'level' => ILogger::INFO,
- ],
- $context
- ));
- } else {
- $this->logger->info($message, $context);
- }
+ public function info($message, array $context = []): void {
+ $this->log(LogLevel::INFO, (string)$message, $context);
}
/**
* Detailed debug information.
*
- * @param string $message
- * @param array $context
- *
- * @return void
+ * @param $message
+ * @param mixed[] $context
*/
- public function debug($message, array $context = []) {
- if ($this->containsThrowable($context)) {
- $this->logger->logException($context['exception'], array_merge(
- [
- 'message' => $message,
- 'level' => ILogger::DEBUG,
- ],
- $context
- ));
- } else {
- $this->logger->debug($message, $context);
- }
+ public function debug($message, array $context = []): void {
+ $this->log(LogLevel::DEBUG, (string)$message, $context);
}
/**
* Logs with an arbitrary level.
*
* @param mixed $level
- * @param string $message
- * @param array $context
- *
- * @return void
+ * @param $message
+ * @param mixed[] $context
*
* @throws InvalidArgumentException
*/
- public function log($level, $message, array $context = []) {
+ public function log($level, $message, array $context = []): void {
+ if (is_string($level)) {
+ $level = self::logLevelToInt($level);
+ }
+ if (isset($context['level']) && is_string($context['level'])) {
+ $context['level'] = self::logLevelToInt($context['level']);
+ }
if (!is_int($level) || $level < ILogger::DEBUG || $level > ILogger::FATAL) {
- throw new InvalidArgumentException('Nextcloud allows only integer log levels');
+ throw new InvalidArgumentException('Unsupported custom log level');
}
if ($this->containsThrowable($context)) {
$this->logger->logException($context['exception'], array_merge(
[
- 'message' => $message,
+ 'message' => (string)$message,
'level' => $level,
],
$context
));
} else {
- $this->logger->log($level, $message, $context);
+ $this->logger->log($level, (string)$message, $context);
}
}