use OCP\ILogger;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LoggerInterface;
+use Throwable;
+use function array_key_exists;
+use function array_merge;
final class PsrLoggerAdapter implements LoggerInterface {
$this->logger = $logger;
}
+ private function containsThrowable(array $context): bool {
+ return array_key_exists('exception', $context) && $context['exception'] instanceof Throwable;
+ }
+
/**
* System is unusable.
*
* @return void
*/
public function emergency($message, array $context = []): void {
- $this->logger->emergency($message, $context);
+ if ($this->containsThrowable($context)) {
+ $this->logger->logException($context['exception'], array_merge(
+ [
+ 'message' => $message,
+ 'level' => ILogger::FATAL,
+ ],
+ $context
+ ));
+ } else {
+ $this->logger->emergency($message, $context);
+ }
}
/**
* @return void
*/
public function alert($message, array $context = []) {
- $this->logger->alert($message, $context);
+ if ($this->containsThrowable($context)) {
+ $this->logger->logException($context['exception'], array_merge(
+ [
+ 'message' => $message,
+ 'level' => ILogger::ERROR,
+ ],
+ $context
+ ));
+ } else {
+ $this->logger->alert($message, $context);
+ }
}
/**
* @return void
*/
public function critical($message, array $context = []) {
- $this->logger->critical($message, $context);
+ if ($this->containsThrowable($context)) {
+ $this->logger->logException($context['exception'], array_merge(
+ [
+ 'message' => $message,
+ 'level' => ILogger::ERROR,
+ ],
+ $context
+ ));
+ } else {
+ $this->logger->critical($message, $context);
+ }
}
/**
* @return void
*/
public function error($message, array $context = []) {
- $this->logger->error($message, $context);
+ if ($this->containsThrowable($context)) {
+ $this->logger->logException($context['exception'], array_merge(
+ [
+ 'message' => $message,
+ 'level' => ILogger::ERROR,
+ ],
+ $context
+ ));
+ } else {
+ $this->logger->error($message, $context);
+ }
}
/**
* @return void
*/
public function warning($message, array $context = []) {
- $this->logger->warning($message, $context);
+ if ($this->containsThrowable($context)) {
+ $this->logger->logException($context['exception'], array_merge(
+ [
+ 'message' => $message,
+ 'level' => ILogger::WARN,
+ ],
+ $context
+ ));
+ } else {
+ $this->logger->warning($message, $context);
+ }
}
/**
* @return void
*/
public function notice($message, array $context = []) {
- $this->logger->notice($message, $context);
+ if ($this->containsThrowable($context)) {
+ $this->logger->logException($context['exception'], array_merge(
+ [
+ 'message' => $message,
+ 'level' => ILogger::INFO,
+ ],
+ $context
+ ));
+ } else {
+ $this->logger->notice($message, $context);
+ }
}
/**
* @return void
*/
public function info($message, array $context = []) {
- $this->logger->info($message, $context);
+ if ($this->containsThrowable($context)) {
+ $this->logger->logException($context['exception'], array_merge(
+ [
+ 'message' => $message,
+ 'level' => ILogger::INFO,
+ ],
+ $context
+ ));
+ } else {
+ $this->logger->info($message, $context);
+ }
}
/**
* @return void
*/
public function debug($message, array $context = []) {
- $this->logger->debug($message, $context);
+ if ($this->containsThrowable($context)) {
+ $this->logger->logException($context['exception'], array_merge(
+ [
+ 'message' => $message,
+ 'level' => ILogger::DEBUG,
+ ],
+ $context
+ ));
+ } else {
+ $this->logger->debug($message, $context);
+ }
}
/**
if (!is_int($level) || $level < ILogger::DEBUG || $level > ILogger::FATAL) {
throw new InvalidArgumentException('Nextcloud allows only integer log levels');
}
- $this->logger->log($level, $message, $context);
+ if ($this->containsThrowable($context)) {
+ $this->logger->logException($context['exception'], array_merge(
+ [
+ 'message' => $message,
+ 'level' => $level,
+ ],
+ $context
+ ));
+ } else {
+ $this->logger->log($level, $message, $context);
+ }
}
}