From 2e4aa474a9d5c5508473e51762bbad6960d27c2e Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Mon, 26 Oct 2026 18:12:07 -0100 Subject: [PATCH] feat(logger): lazy logger Signed-off-by: Maxence Lange --- lib/private/Log/LazyLogger.php | 46 +++++++++++++++++++++++++ lib/private/Log/LazyLoggerInterface.php | 24 +++++++++++++ lib/private/Server.php | 1 + lib/public/Log/ILazyLogger.php | 25 ++++++++++++++ lib/public/Log/ILazyLoggerInterface.php | 13 +++++++ 5 files changed, 109 insertions(+) create mode 100644 lib/private/Log/LazyLogger.php create mode 100644 lib/private/Log/LazyLoggerInterface.php create mode 100644 lib/public/Log/ILazyLogger.php create mode 100644 lib/public/Log/ILazyLoggerInterface.php diff --git a/lib/private/Log/LazyLogger.php b/lib/private/Log/LazyLogger.php new file mode 100644 index 0000000000000..9c23334423c0b --- /dev/null +++ b/lib/private/Log/LazyLogger.php @@ -0,0 +1,46 @@ +getSystemValueInt('loglevel', ILogger::WARN); + if ($logLevel === ILogger::DEBUG) { + $this->debug = new LazyLoggerInterface($logger, ILogger::DEBUG); + } + if ($logLevel <= ILogger::INFO) { + $this->info = new LazyLoggerInterface($logger, ILogger::INFO); + } + if ($logLevel <= ILogger::WARN) { + $this->warning = new LazyLoggerInterface($logger, ILogger::WARN); + } + if ($logLevel <= ILogger::ERROR) { + $this->error = new LazyLoggerInterface($logger, ILogger::ERROR); + } + $this->fatal = new LazyLoggerInterface($logger, ILogger::FATAL); + } +} diff --git a/lib/private/Log/LazyLoggerInterface.php b/lib/private/Log/LazyLoggerInterface.php new file mode 100644 index 0000000000000..fe5943521241f --- /dev/null +++ b/lib/private/Log/LazyLoggerInterface.php @@ -0,0 +1,24 @@ +logger->log($this->level, $message, $context); + } +} diff --git a/lib/private/Server.php b/lib/private/Server.php index 9d6dde25f4220..e9c57741e0e6a 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -576,6 +576,7 @@ public function __construct( }); // PSR-3 logger $this->registerAlias(LoggerInterface::class, PsrLoggerAdapter::class); + $this->registerAlias(\OCP\Log\ILazyLogger::class, \OC\Log\LazyLogger::class); $this->registerAlias(ILogFactory::class, LogFactory::class); $this->registerAlias(IJobList::class, JobList::class); diff --git a/lib/public/Log/ILazyLogger.php b/lib/public/Log/ILazyLogger.php new file mode 100644 index 0000000000000..0c838b18c78c0 --- /dev/null +++ b/lib/public/Log/ILazyLogger.php @@ -0,0 +1,25 @@ +logger->debug?->print('my message', ['data' => my_heavy_process()]); + * + * if loglevel does not allow it, the payload will not be loaded/initialized + * + * @since 39.0.0 + */ +interface ILazyLogger { + public ?ILazyLoggerInterface $debug { get; } + public ?ILazyLoggerInterface $info { get; } + public ?ILazyLoggerInterface $warning { get; } + public ?ILazyLoggerInterface $error { get; } + public ?ILazyLoggerInterface $fatal { get; } +} diff --git a/lib/public/Log/ILazyLoggerInterface.php b/lib/public/Log/ILazyLoggerInterface.php new file mode 100644 index 0000000000000..c0e092ba70ce7 --- /dev/null +++ b/lib/public/Log/ILazyLoggerInterface.php @@ -0,0 +1,13 @@ +