Skip to content
Draft
Show file tree
Hide file tree
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
46 changes: 46 additions & 0 deletions lib/private/Log/LazyLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Log;

use OCP\IConfig;
use OCP\ILogger;
use OCP\Log\ILazyLogger;
use Psr\Log\LoggerInterface;

/**
* @inheritDoc
* @since 39.0.0
*/
class LazyLogger implements ILazyLogger {
public ?LazyLoggerInterface $debug = null;
public ?LazyLoggerInterface $info = null;
public ?LazyLoggerInterface $warning = null;
public ?LazyLoggerInterface $error = null;
public ?LazyLoggerInterface $fatal = null;

public function __construct(
LoggerInterface $logger,
IConfig $config,
) {
$logLevel = $config->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);
}
}
24 changes: 24 additions & 0 deletions lib/private/Log/LazyLoggerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Log;

use OCP\Log\ILazyLoggerInterface;
use Psr\Log\LoggerInterface;

class LazyLoggerInterface implements ILazyLoggerInterface {
public function __construct(
private readonly LoggerInterface $logger,
private readonly int $level,
) {
}

public function print(string $message, array $context = []): void {

Check failure on line 21 in lib/private/Log/LazyLoggerInterface.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

MissingOverrideAttribute

lib/private/Log/LazyLoggerInterface.php:21:2: MissingOverrideAttribute: Method OC\Log\LazyLoggerInterface::print should have the "Override" attribute (see https://psalm.dev/358)
$this->logger->log($this->level, $message, $context);
}
}
1 change: 1 addition & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 25 additions & 0 deletions lib/public/Log/ILazyLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCP\Log;

/**
* will only initialize the payload if needed:
* $this->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; }

Check failure on line 20 in lib/public/Log/ILazyLogger.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

ParseError

lib/public/Log/ILazyLogger.php:20:2: ParseError: Interfaces cannot have properties (see https://psalm.dev/173)

Check failure on line 20 in lib/public/Log/ILazyLogger.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

ParseError

lib/public/Log/ILazyLogger.php:20:2: ParseError: Interfaces cannot have properties (see https://psalm.dev/173)
public ?ILazyLoggerInterface $info { get; }
public ?ILazyLoggerInterface $warning { get; }
public ?ILazyLoggerInterface $error { get; }
public ?ILazyLoggerInterface $fatal { get; }
}
13 changes: 13 additions & 0 deletions lib/public/Log/ILazyLoggerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCP\Log;

interface ILazyLoggerInterface {

Check failure on line 11 in lib/public/Log/ILazyLoggerInterface.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

InvalidDocblock

lib/public/Log/ILazyLoggerInterface.php:11:1: InvalidDocblock: PHPDoc is required for classes/interfaces in OCP. (see https://psalm.dev/008)
public function print(string $message, array $context = []): void;

Check failure on line 12 in lib/public/Log/ILazyLoggerInterface.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

InvalidDocblock

lib/public/Log/ILazyLoggerInterface.php:12:2: InvalidDocblock: PHPDoc is required for methods in OCP. (see https://psalm.dev/008)
}
Loading