aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Log
diff options
context:
space:
mode:
authorFerdinand Thiessen <opensource@fthiessen.de>2024-08-17 00:25:08 +0200
committerFerdinand Thiessen <opensource@fthiessen.de>2024-09-14 17:35:11 +0200
commit81df2d2c12917ec9d442f2f5362f0e5d609a830b (patch)
treef22c31cbde974b0c1920926a80abd3c32c0a929d /lib/private/Log
parenta751ff7b93039b9e24e6a29fec50482dd07f7dbf (diff)
downloadnextcloud-server-81df2d2c12917ec9d442f2f5362f0e5d609a830b.tar.gz
nextcloud-server-81df2d2c12917ec9d442f2f5362f0e5d609a830b.zip
feat(PsrLoggerAdapter): Allow to use `Psr\Log\LogLevel` for `log` method
There is the `Psr\Log\LogLevel` class defining loglevel constants, to be fully compatible we should at least support those logging levels. Moreover this is the last part that was still required from `ILogger` interface, as we did not have alternatives for the loglevel constants. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
Diffstat (limited to 'lib/private/Log')
-rw-r--r--lib/private/Log/PsrLoggerAdapter.php119
1 files changed, 30 insertions, 89 deletions
diff --git a/lib/private/Log/PsrLoggerAdapter.php b/lib/private/Log/PsrLoggerAdapter.php
index 57a67bd67a7..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);
}
@@ -39,17 +54,7 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
* @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);
}
/**
@@ -62,17 +67,7 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
* @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);
}
/**
@@ -84,17 +79,7 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
* @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);
}
/**
@@ -105,17 +90,7 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
* @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);
}
/**
@@ -128,17 +103,7 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
* @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);
}
/**
@@ -148,17 +113,7 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
* @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);
}
/**
@@ -170,17 +125,7 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
* @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);
}
/**
@@ -190,17 +135,7 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
* @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);
}
/**
@@ -213,8 +148,14 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
* @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(