Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
feat: log app version
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Sep 11, 2025
commit d1008a5590ece2d06a1a4b7a7b64b0c7a7cfcbbb
18 changes: 17 additions & 1 deletion lib/private/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Nextcloud\LogNormalizer\Normalizer;
use OC\AppFramework\Bootstrap\Coordinator;
use OC\Log\ExceptionSerializer;
use OCP\App\IAppManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ILogger;
use OCP\IRequest;
Expand All @@ -38,13 +39,26 @@ class Log implements ILogger, IDataLogger {
private ?bool $logConditionSatisfied = null;
private ?IEventDispatcher $eventDispatcher = null;
private int $nestingLevel = 0;
/** @var callable(): IAppManager */
private $appManagerFactory;
private ?IAppManager $appManager = null;

public function __construct(
private IWriter $logger,
private SystemConfig $config,
private Normalizer $normalizer = new Normalizer(),
private ?IRegistry $crashReporters = null,
?callable $appManagerFactory = null,
) {
$this->appManagerFactory = $appManagerFactory ?? fn() => \OCP\Server::get(IAppManager::class);
}

private function getAppManager(): IAppManager {
if (!$this->appManager) {
$this->appManager = ($this->appManagerFactory)();
}

return $this->appManager;
}

public function setEventDispatcher(IEventDispatcher $eventDispatcher): void {
Expand Down Expand Up @@ -142,7 +156,6 @@ public function debug(string $message, array $context = []): void {
$this->log(ILogger::DEBUG, $message, $context);
}


/**
* Logs with an arbitrary level.
*
Expand All @@ -158,6 +171,9 @@ public function log(int $level, string $message, array $context = []): void {
return; // no crash reporter, no listeners, we can stop for lower log level
}

if (isset($context['app']) && $version = $this->getAppManager()->getAppVersion($context['app'])) {
$context['app_version'] = $version;
}
array_walk($context, [$this->normalizer, 'format']);

$app = $context['app'] ?? 'no app in context';
Expand Down
1 change: 1 addition & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ public function __construct($webRoot, \OC\Config $config) {
$factory = new LogFactory($c, $this->get(SystemConfig::class));
$logger = $factory->get($logType);
$registry = $c->get(\OCP\Support\CrashReport\IRegistry::class);
$appManagerFactory = fn() => $c->get(IAppManager::class);

return new Log($logger, $this->get(SystemConfig::class), crashReporters: $registry);
});
Expand Down
Loading