mirror of
https://github.com/nextcloud/server.git
synced 2026-06-13 10:40:40 -04:00
fix: make the PsrLoggerAdapter load the real logger lazily
Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
7b85dacfd5
commit
e3f97eac11
2 changed files with 20 additions and 5 deletions
|
|
@ -20,11 +20,23 @@ use function array_key_exists;
|
|||
use function array_merge;
|
||||
|
||||
final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
|
||||
private ?Log $logger = null;
|
||||
|
||||
/**
|
||||
* @param \Closure(): Log $loggerClosure
|
||||
*/
|
||||
public function __construct(
|
||||
private Log $logger,
|
||||
private $loggerClosure,
|
||||
) {
|
||||
}
|
||||
|
||||
private function getLogger(): Log {
|
||||
if (!$this->logger) {
|
||||
$this->logger = ($this->loggerClosure)();
|
||||
}
|
||||
return $this->logger;
|
||||
}
|
||||
|
||||
public static function logLevelToInt(string $level): int {
|
||||
return match ($level) {
|
||||
LogLevel::ALERT => ILogger::ERROR,
|
||||
|
|
@ -40,7 +52,7 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
|
|||
}
|
||||
|
||||
public function setEventDispatcher(IEventDispatcher $eventDispatcher): void {
|
||||
$this->logger->setEventDispatcher($eventDispatcher);
|
||||
$this->getLogger()->setEventDispatcher($eventDispatcher);
|
||||
}
|
||||
|
||||
private function containsThrowable(array $context): bool {
|
||||
|
|
@ -158,7 +170,7 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
|
|||
throw new InvalidArgumentException('Unsupported custom log level');
|
||||
}
|
||||
if ($this->containsThrowable($context)) {
|
||||
$this->logger->logException($context['exception'], array_merge(
|
||||
$this->getLogger()->logException($context['exception'], array_merge(
|
||||
[
|
||||
'message' => (string)$message,
|
||||
'level' => $level,
|
||||
|
|
@ -166,11 +178,11 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
|
|||
$context
|
||||
));
|
||||
} else {
|
||||
$this->logger->log($level, (string)$message, $context);
|
||||
$this->getLogger()->log($level, (string)$message, $context);
|
||||
}
|
||||
}
|
||||
|
||||
public function logData(string $message, array $data, array $context = []): void {
|
||||
$this->logger->logData($message, $data, $context);
|
||||
$this->getLogger()->logData($message, $data, $context);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -674,6 +674,9 @@ class Server extends ServerContainer implements IServerContainer {
|
|||
});
|
||||
// PSR-3 logger
|
||||
$this->registerAlias(LoggerInterface::class, PsrLoggerAdapter::class);
|
||||
$this->registerService(PsrLoggerAdapter::class, function (Server $c) {
|
||||
return new PsrLoggerAdapter(fn () => $c->get(Log::class));
|
||||
});
|
||||
|
||||
$this->registerService(ILogFactory::class, function (Server $c) {
|
||||
return new LogFactory($c, $this->get(SystemConfig::class));
|
||||
|
|
|
|||
Loading…
Reference in a new issue