2015-08-17 23:16:39 +01:00
|
|
|
<?php
|
2024-05-24 19:43:47 +02:00
|
|
|
|
2015-08-17 23:16:39 +01:00
|
|
|
/**
|
2024-05-24 19:43:47 +02:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-08-17 23:16:39 +01:00
|
|
|
*/
|
|
|
|
namespace OC\Core\Command\Log;
|
|
|
|
|
2019-11-22 20:52:10 +01:00
|
|
|
use OCP\IConfig;
|
2015-08-17 23:16:39 +01:00
|
|
|
|
2016-09-21 14:21:39 +02:00
|
|
|
use Stecman\Component\Symfony\Console\BashCompletion\Completion;
|
|
|
|
use Stecman\Component\Symfony\Console\BashCompletion\Completion\ShellPathCompletion;
|
|
|
|
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
|
2015-08-17 23:16:39 +01:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
2016-09-21 14:21:39 +02:00
|
|
|
class File extends Command implements Completion\CompletionAwareInterface {
|
2023-06-13 16:06:16 +03:30
|
|
|
public function __construct(
|
|
|
|
protected IConfig $config,
|
|
|
|
) {
|
2015-08-17 23:16:39 +01:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function configure() {
|
|
|
|
$this
|
2016-07-22 11:44:19 +02:00
|
|
|
->setName('log:file')
|
|
|
|
->setDescription('manipulate logging backend')
|
2015-08-17 23:16:39 +01:00
|
|
|
->addOption(
|
|
|
|
'enable',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'enable this logging backend'
|
|
|
|
)
|
|
|
|
->addOption(
|
|
|
|
'file',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_REQUIRED,
|
|
|
|
'set the log file path'
|
|
|
|
)
|
|
|
|
->addOption(
|
|
|
|
'rotate-size',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_REQUIRED,
|
|
|
|
'set the file size for log rotation, 0 = disabled'
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2020-06-26 14:54:51 +02:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int {
|
2015-08-17 23:16:39 +01:00
|
|
|
$toBeSet = [];
|
|
|
|
|
|
|
|
if ($input->getOption('enable')) {
|
2016-07-22 11:44:19 +02:00
|
|
|
$toBeSet['log_type'] = 'file';
|
2015-08-17 23:16:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($file = $input->getOption('file')) {
|
|
|
|
$toBeSet['logfile'] = $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($rotateSize = $input->getOption('rotate-size')) !== null) {
|
|
|
|
$rotateSize = \OCP\Util::computerFileSize($rotateSize);
|
|
|
|
$this->validateRotateSize($rotateSize);
|
|
|
|
$toBeSet['log_rotate_size'] = $rotateSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set config
|
|
|
|
foreach ($toBeSet as $option => $value) {
|
|
|
|
$this->config->setSystemValue($option, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// display config
|
2016-07-22 11:44:19 +02:00
|
|
|
// TODO: Drop backwards compatibility for config in the future
|
|
|
|
$logType = $this->config->getSystemValue('log_type', 'file');
|
|
|
|
if ($logType === 'file' || $logType === 'owncloud') {
|
2015-08-17 23:16:39 +01:00
|
|
|
$enabledText = 'enabled';
|
|
|
|
} else {
|
|
|
|
$enabledText = 'disabled';
|
|
|
|
}
|
2016-07-22 11:44:19 +02:00
|
|
|
$output->writeln('Log backend file: '.$enabledText);
|
2015-08-17 23:16:39 +01:00
|
|
|
|
|
|
|
$dataDir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data');
|
2016-07-04 11:50:32 +02:00
|
|
|
$defaultLogFile = rtrim($dataDir, '/').'/nextcloud.log';
|
2015-08-17 23:16:39 +01:00
|
|
|
$output->writeln('Log file: '.$this->config->getSystemValue('logfile', $defaultLogFile));
|
|
|
|
|
2020-10-05 15:12:57 +02:00
|
|
|
$rotateSize = $this->config->getSystemValue('log_rotate_size', 100 * 1024 * 1024);
|
2015-08-17 23:16:39 +01:00
|
|
|
if ($rotateSize) {
|
|
|
|
$rotateString = \OCP\Util::humanFileSize($rotateSize);
|
|
|
|
} else {
|
|
|
|
$rotateString = 'disabled';
|
|
|
|
}
|
|
|
|
$output->writeln('Rotate at: '.$rotateString);
|
2020-06-26 14:54:51 +02:00
|
|
|
return 0;
|
2015-08-17 23:16:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
2023-01-23 12:13:32 +01:00
|
|
|
protected function validateRotateSize(false|int|float $rotateSize): void {
|
2015-08-17 23:16:39 +01:00
|
|
|
if ($rotateSize === false) {
|
|
|
|
throw new \InvalidArgumentException('Error parsing log rotation file size');
|
|
|
|
}
|
|
|
|
if ($rotateSize < 0) {
|
|
|
|
throw new \InvalidArgumentException('Log rotation file size must be non-negative');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 14:21:39 +02:00
|
|
|
/**
|
|
|
|
* @param string $optionName
|
|
|
|
* @param CompletionContext $context
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function completeOptionValues($optionName, CompletionContext $context) {
|
|
|
|
if ($optionName === 'file') {
|
|
|
|
$helper = new ShellPathCompletion(
|
|
|
|
$this->getName(),
|
|
|
|
'file',
|
|
|
|
Completion::TYPE_OPTION
|
|
|
|
);
|
|
|
|
return $helper->run();
|
2020-04-10 10:35:09 +02:00
|
|
|
} elseif ($optionName === 'rotate-size') {
|
2016-09-21 14:21:39 +02:00
|
|
|
return [0];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $argumentName
|
|
|
|
* @param CompletionContext $context
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function completeArgumentValues($argumentName, CompletionContext $context) {
|
|
|
|
return [];
|
|
|
|
}
|
2015-08-17 23:16:39 +01:00
|
|
|
}
|