Skip to content
Open
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
8 changes: 7 additions & 1 deletion lib/private/Log/Rotate.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use OCP\IConfig;
use OCP\Log\IFileBased;
use OCP\Log\ILogFactory;
use OCP\Log\RotationTrait;
use OCP\Server;
use Psr\Log\LoggerInterface;
Expand All @@ -33,7 +35,11 @@ public function __construct(ITimeFactory $time) {
#[\Override]
public function run($argument): void {
$config = Server::get(IConfig::class);
$this->filePath = $config->getSystemValueString('logfile', $config->getSystemValueString('datadirectory', \OC::$SERVERROOT . '/data') . '/nextcloud.log');
$logFile = Server::get(ILogFactory::class)->get('file');
if (!$logFile instanceof IFileBased) {
return;
}
$this->filePath = $logFile->getLogFilePath();

$this->maxSize = $config->getSystemValueInt('log_rotate_size', 100 * 1024 * 1024);
if ($this->shouldRotateBySize()) {
Expand Down
56 changes: 56 additions & 0 deletions tests/lib/Log/RotateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace Test\Log;

use OC\Log\File;
use OC\Log\Rotate;
use OC\SystemConfig;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
use OCP\Log\ILogFactory;
use Test\TestCase;

class RotateTest extends TestCase {
private string $dir;

#[\Override]
protected function setUp(): void {
parent::setUp();
$this->dir = sys_get_temp_dir() . '/' . self::getUniqueID('rotate');
mkdir($this->dir);
}

#[\Override]
protected function tearDown(): void {
@unlink($this->dir . '/nextcloud.log');
@unlink($this->dir . '/nextcloud.log.1');
@rmdir($this->dir);
parent::tearDown();
}

public function testRotatesFallbackLogFile(): void {
$systemConfig = $this->createMock(SystemConfig::class);
$systemConfig->method('getValue')->willReturnArgument(1);
$logFile = new File($this->dir . '/missing/nextcloud.log', $this->dir . '/nextcloud.log', $systemConfig);
file_put_contents($this->dir . '/nextcloud.log', 'log entry');

$logFactory = $this->createMock(ILogFactory::class);
$logFactory->method('get')->with('file')->willReturn($logFile);
$this->overwriteService(ILogFactory::class, $logFactory);

$config = $this->createMock(IConfig::class);
$config->method('getSystemValueInt')->with('log_rotate_size')->willReturn(1);
$this->overwriteService(IConfig::class, $config);

$job = new Rotate($this->createMock(ITimeFactory::class));
$job->run(null);

$this->assertFileDoesNotExist($this->dir . '/nextcloud.log');
$this->assertFileExists($this->dir . '/nextcloud.log.1');
}
}