File.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin McCorkell <robin@mccorkell.me.uk>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Pulzer <t.pulzer@kniel.de>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Core\Command\Log;
  27. use OCP\IConfig;
  28. use Stecman\Component\Symfony\Console\BashCompletion\Completion;
  29. use Stecman\Component\Symfony\Console\BashCompletion\Completion\ShellPathCompletion;
  30. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  31. use Symfony\Component\Console\Command\Command;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Input\InputOption;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. class File extends Command implements Completion\CompletionAwareInterface {
  36. /** @var IConfig */
  37. protected $config;
  38. public function __construct(IConfig $config) {
  39. $this->config = $config;
  40. parent::__construct();
  41. }
  42. protected function configure() {
  43. $this
  44. ->setName('log:file')
  45. ->setDescription('manipulate logging backend')
  46. ->addOption(
  47. 'enable',
  48. null,
  49. InputOption::VALUE_NONE,
  50. 'enable this logging backend'
  51. )
  52. ->addOption(
  53. 'file',
  54. null,
  55. InputOption::VALUE_REQUIRED,
  56. 'set the log file path'
  57. )
  58. ->addOption(
  59. 'rotate-size',
  60. null,
  61. InputOption::VALUE_REQUIRED,
  62. 'set the file size for log rotation, 0 = disabled'
  63. )
  64. ;
  65. }
  66. protected function execute(InputInterface $input, OutputInterface $output) {
  67. $toBeSet = [];
  68. if ($input->getOption('enable')) {
  69. $toBeSet['log_type'] = 'file';
  70. }
  71. if ($file = $input->getOption('file')) {
  72. $toBeSet['logfile'] = $file;
  73. }
  74. if (($rotateSize = $input->getOption('rotate-size')) !== null) {
  75. $rotateSize = \OCP\Util::computerFileSize($rotateSize);
  76. $this->validateRotateSize($rotateSize);
  77. $toBeSet['log_rotate_size'] = $rotateSize;
  78. }
  79. // set config
  80. foreach ($toBeSet as $option => $value) {
  81. $this->config->setSystemValue($option, $value);
  82. }
  83. // display config
  84. // TODO: Drop backwards compatibility for config in the future
  85. $logType = $this->config->getSystemValue('log_type', 'file');
  86. if ($logType === 'file' || $logType === 'owncloud') {
  87. $enabledText = 'enabled';
  88. } else {
  89. $enabledText = 'disabled';
  90. }
  91. $output->writeln('Log backend file: '.$enabledText);
  92. $dataDir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data');
  93. $defaultLogFile = rtrim($dataDir, '/').'/nextcloud.log';
  94. $output->writeln('Log file: '.$this->config->getSystemValue('logfile', $defaultLogFile));
  95. $rotateSize = $this->config->getSystemValue('log_rotate_size', 100*1024*1024);
  96. if ($rotateSize) {
  97. $rotateString = \OCP\Util::humanFileSize($rotateSize);
  98. } else {
  99. $rotateString = 'disabled';
  100. }
  101. $output->writeln('Rotate at: '.$rotateString);
  102. }
  103. /**
  104. * @param mixed $rotateSize
  105. * @throws \InvalidArgumentException
  106. */
  107. protected function validateRotateSize(&$rotateSize) {
  108. if ($rotateSize === false) {
  109. throw new \InvalidArgumentException('Error parsing log rotation file size');
  110. }
  111. $rotateSize = (int) $rotateSize;
  112. if ($rotateSize < 0) {
  113. throw new \InvalidArgumentException('Log rotation file size must be non-negative');
  114. }
  115. }
  116. /**
  117. * @param string $optionName
  118. * @param CompletionContext $context
  119. * @return string[]
  120. */
  121. public function completeOptionValues($optionName, CompletionContext $context) {
  122. if ($optionName === 'file') {
  123. $helper = new ShellPathCompletion(
  124. $this->getName(),
  125. 'file',
  126. Completion::TYPE_OPTION
  127. );
  128. return $helper->run();
  129. } else if ($optionName === 'rotate-size') {
  130. return [0];
  131. }
  132. return [];
  133. }
  134. /**
  135. * @param string $argumentName
  136. * @param CompletionContext $context
  137. * @return string[]
  138. */
  139. public function completeArgumentValues($argumentName, CompletionContext $context) {
  140. return [];
  141. }
  142. }