Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 106 additions & 9 deletions lib/private/Log/PsrLoggerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
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 {

Expand All @@ -39,6 +42,10 @@ public function __construct(ILogger $logger) {
$this->logger = $logger;
}

private function containsThrowable(array $context): bool {
return array_key_exists('exception', $context) && $context['exception'] instanceof Throwable;
}

/**
* System is unusable.
*
Expand All @@ -48,7 +55,17 @@ public function __construct(ILogger $logger) {
* @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);
}
}

/**
Expand All @@ -63,7 +80,17 @@ public function emergency($message, array $context = []): void {
* @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);
}
}

/**
Expand All @@ -77,7 +104,17 @@ public function alert($message, array $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);
}
}

/**
Expand All @@ -90,7 +127,17 @@ public function critical($message, array $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);
}
}

/**
Expand All @@ -105,7 +152,17 @@ public function error($message, array $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);
}
}

/**
Expand All @@ -117,7 +174,17 @@ public function warning($message, array $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);
}
}

/**
Expand All @@ -131,7 +198,17 @@ public function notice($message, array $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);
}
}

/**
Expand All @@ -143,7 +220,17 @@ public function info($message, array $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);
}
}

/**
Expand All @@ -161,6 +248,16 @@ public function log($level, $message, array $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);
}
}
}