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.php137
1 files changed, 39 insertions, 98 deletions
diff --git a/lib/private/Log/PsrLoggerAdapter.php b/lib/private/Log/PsrLoggerAdapter.php
index 16e609eefdb..79e9d4d9621 100644
--- a/lib/private/Log/PsrLoggerAdapter.php
+++ b/lib/private/Log/PsrLoggerAdapter.php
@@ -14,6 +14,7 @@ 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;
@@ -24,6 +25,20 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
) {
}
+ 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 setEventDispatcher(IEventDispatcher $eventDispatcher): void {
$this->logger->setEventDispatcher($eventDispatcher);
}
@@ -35,21 +50,11 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
/**
* System is unusable.
*
- * @param $message
+ * @param $message
* @param mixed[] $context
*/
public function emergency($message, array $context = []): void {
- if ($this->containsThrowable($context)) {
- $this->logger->logException($context['exception'], array_merge(
- [
- 'message' => (string)$message,
- 'level' => ILogger::FATAL,
- ],
- $context
- ));
- } else {
- $this->logger->emergency((string)$message, $context);
- }
+ $this->log(LogLevel::EMERGENCY, (string)$message, $context);
}
/**
@@ -58,21 +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 $message
+ * @param $message
* @param mixed[] $context
*/
public function alert($message, array $context = []): void {
- if ($this->containsThrowable($context)) {
- $this->logger->logException($context['exception'], array_merge(
- [
- 'message' => (string)$message,
- 'level' => ILogger::ERROR,
- ],
- $context
- ));
- } else {
- $this->logger->alert((string)$message, $context);
- }
+ $this->log(LogLevel::ALERT, (string)$message, $context);
}
/**
@@ -80,42 +75,22 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
*
* Example: Application component unavailable, unexpected exception.
*
- * @param $message
+ * @param $message
* @param mixed[] $context
*/
public function critical($message, array $context = []): void {
- if ($this->containsThrowable($context)) {
- $this->logger->logException($context['exception'], array_merge(
- [
- 'message' => (string)$message,
- 'level' => ILogger::ERROR,
- ],
- $context
- ));
- } else {
- $this->logger->critical((string)$message, $context);
- }
+ $this->log(LogLevel::CRITICAL, (string)$message, $context);
}
/**
* Runtime errors that do not require immediate action but should typically
* be logged and monitored.
*
- * @param $message
+ * @param $message
* @param mixed[] $context
*/
public function error($message, array $context = []): void {
- if ($this->containsThrowable($context)) {
- $this->logger->logException($context['exception'], array_merge(
- [
- 'message' => (string)$message,
- 'level' => ILogger::ERROR,
- ],
- $context
- ));
- } else {
- $this->logger->error((string)$message, $context);
- }
+ $this->log(LogLevel::ERROR, (string)$message, $context);
}
/**
@@ -124,41 +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 $message
+ * @param $message
* @param mixed[] $context
*/
public function warning($message, array $context = []): void {
- if ($this->containsThrowable($context)) {
- $this->logger->logException($context['exception'], array_merge(
- [
- 'message' => (string)$message,
- 'level' => ILogger::WARN,
- ],
- $context
- ));
- } else {
- $this->logger->warning((string)$message, $context);
- }
+ $this->log(LogLevel::WARNING, (string)$message, $context);
}
/**
* Normal but significant events.
*
- * @param $message
+ * @param $message
* @param mixed[] $context
*/
public function notice($message, array $context = []): void {
- if ($this->containsThrowable($context)) {
- $this->logger->logException($context['exception'], array_merge(
- [
- 'message' => (string)$message,
- 'level' => ILogger::INFO,
- ],
- $context
- ));
- } else {
- $this->logger->notice((string)$message, $context);
- }
+ $this->log(LogLevel::NOTICE, (string)$message, $context);
}
/**
@@ -166,55 +121,41 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
*
* Example: User logs in, SQL logs.
*
- * @param $message
+ * @param $message
* @param mixed[] $context
*/
public function info($message, array $context = []): void {
- if ($this->containsThrowable($context)) {
- $this->logger->logException($context['exception'], array_merge(
- [
- 'message' => (string)$message,
- 'level' => ILogger::INFO,
- ],
- $context
- ));
- } else {
- $this->logger->info((string)$message, $context);
- }
+ $this->log(LogLevel::INFO, (string)$message, $context);
}
/**
* Detailed debug information.
*
- * @param $message
+ * @param $message
* @param mixed[] $context
*/
public function debug($message, array $context = []): void {
- if ($this->containsThrowable($context)) {
- $this->logger->logException($context['exception'], array_merge(
- [
- 'message' => (string)$message,
- 'level' => ILogger::DEBUG,
- ],
- $context
- ));
- } else {
- $this->logger->debug((string)$message, $context);
- }
+ $this->log(LogLevel::DEBUG, (string)$message, $context);
}
/**
* Logs with an arbitrary level.
*
* @param mixed $level
- * @param $message
+ * @param $message
* @param mixed[] $context
*
* @throws InvalidArgumentException
*/
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(