Manage.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Command\Log;
  8. use OCP\IConfig;
  9. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  10. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. class Manage extends Command implements CompletionAwareInterface {
  16. public const DEFAULT_BACKEND = 'file';
  17. public const DEFAULT_LOG_LEVEL = 2;
  18. public const DEFAULT_TIMEZONE = 'UTC';
  19. public function __construct(
  20. protected IConfig $config,
  21. ) {
  22. parent::__construct();
  23. }
  24. protected function configure() {
  25. $this
  26. ->setName('log:manage')
  27. ->setDescription('manage logging configuration')
  28. ->addOption(
  29. 'backend',
  30. null,
  31. InputOption::VALUE_REQUIRED,
  32. 'set the logging backend [file, syslog, errorlog, systemd]'
  33. )
  34. ->addOption(
  35. 'level',
  36. null,
  37. InputOption::VALUE_REQUIRED,
  38. 'set the log level [debug, info, warning, error, fatal]'
  39. )
  40. ->addOption(
  41. 'timezone',
  42. null,
  43. InputOption::VALUE_REQUIRED,
  44. 'set the logging timezone'
  45. )
  46. ;
  47. }
  48. protected function execute(InputInterface $input, OutputInterface $output): int {
  49. // collate config setting to the end, to avoid partial configuration
  50. $toBeSet = [];
  51. if ($backend = $input->getOption('backend')) {
  52. $this->validateBackend($backend);
  53. $toBeSet['log_type'] = $backend;
  54. }
  55. $level = $input->getOption('level');
  56. if ($level !== null) {
  57. if (is_numeric($level)) {
  58. $levelNum = $level;
  59. // sanity check
  60. $this->convertLevelNumber($levelNum);
  61. } else {
  62. $levelNum = $this->convertLevelString($level);
  63. }
  64. $toBeSet['loglevel'] = $levelNum;
  65. }
  66. if ($timezone = $input->getOption('timezone')) {
  67. $this->validateTimezone($timezone);
  68. $toBeSet['logtimezone'] = $timezone;
  69. }
  70. // set config
  71. foreach ($toBeSet as $option => $value) {
  72. $this->config->setSystemValue($option, $value);
  73. }
  74. // display configuration
  75. $backend = $this->config->getSystemValue('log_type', self::DEFAULT_BACKEND);
  76. $output->writeln('Enabled logging backend: '.$backend);
  77. $levelNum = $this->config->getSystemValue('loglevel', self::DEFAULT_LOG_LEVEL);
  78. $level = $this->convertLevelNumber($levelNum);
  79. $output->writeln('Log level: '.$level.' ('.$levelNum.')');
  80. $timezone = $this->config->getSystemValue('logtimezone', self::DEFAULT_TIMEZONE);
  81. $output->writeln('Log timezone: '.$timezone);
  82. return 0;
  83. }
  84. /**
  85. * @param string $backend
  86. * @throws \InvalidArgumentException
  87. */
  88. protected function validateBackend($backend) {
  89. if (!class_exists('OC\\Log\\'.ucfirst($backend))) {
  90. throw new \InvalidArgumentException('Invalid backend');
  91. }
  92. }
  93. /**
  94. * @param string $timezone
  95. * @throws \Exception
  96. */
  97. protected function validateTimezone($timezone) {
  98. new \DateTimeZone($timezone);
  99. }
  100. /**
  101. * @param string $level
  102. * @return int
  103. * @throws \InvalidArgumentException
  104. */
  105. protected function convertLevelString($level) {
  106. $level = strtolower($level);
  107. switch ($level) {
  108. case 'debug':
  109. return 0;
  110. case 'info':
  111. return 1;
  112. case 'warning':
  113. case 'warn':
  114. return 2;
  115. case 'error':
  116. case 'err':
  117. return 3;
  118. case 'fatal':
  119. return 4;
  120. }
  121. throw new \InvalidArgumentException('Invalid log level string');
  122. }
  123. /**
  124. * @param int $levelNum
  125. * @return string
  126. * @throws \InvalidArgumentException
  127. */
  128. protected function convertLevelNumber($levelNum) {
  129. switch ($levelNum) {
  130. case 0:
  131. return 'Debug';
  132. case 1:
  133. return 'Info';
  134. case 2:
  135. return 'Warning';
  136. case 3:
  137. return 'Error';
  138. case 4:
  139. return 'Fatal';
  140. }
  141. throw new \InvalidArgumentException('Invalid log level number');
  142. }
  143. /**
  144. * @param string $optionName
  145. * @param CompletionContext $context
  146. * @return string[]
  147. */
  148. public function completeOptionValues($optionName, CompletionContext $context) {
  149. if ($optionName === 'backend') {
  150. return ['file', 'syslog', 'errorlog', 'systemd'];
  151. } elseif ($optionName === 'level') {
  152. return ['debug', 'info', 'warning', 'error', 'fatal'];
  153. } elseif ($optionName === 'timezone') {
  154. return \DateTimeZone::listIdentifiers();
  155. }
  156. return [];
  157. }
  158. /**
  159. * @param string $argumentName
  160. * @param CompletionContext $context
  161. * @return string[]
  162. */
  163. public function completeArgumentValues($argumentName, CompletionContext $context) {
  164. return [];
  165. }
  166. }