diff --git a/lib/private/Log/Rotate.php b/lib/private/Log/Rotate.php index 6a78852d4c774..3627bc49d1f10 100644 --- a/lib/private/Log/Rotate.php +++ b/lib/private/Log/Rotate.php @@ -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; @@ -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()) { diff --git a/tests/lib/Log/RotateTest.php b/tests/lib/Log/RotateTest.php new file mode 100644 index 0000000000000..a4b14d06812f4 --- /dev/null +++ b/tests/lib/Log/RotateTest.php @@ -0,0 +1,56 @@ +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'); + } +}