Manage.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Johannes Ernst <jernst@indiecomputing.com>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Tim Terhorst <mynamewastaken+gitlab@gmail.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Core\Command\Log;
  28. use OCP\IConfig;
  29. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  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 Manage extends Command implements CompletionAwareInterface {
  36. public const DEFAULT_BACKEND = 'file';
  37. public const DEFAULT_LOG_LEVEL = 2;
  38. public const DEFAULT_TIMEZONE = 'UTC';
  39. protected IConfig $config;
  40. public function __construct(IConfig $config) {
  41. $this->config = $config;
  42. parent::__construct();
  43. }
  44. protected function configure() {
  45. $this
  46. ->setName('log:manage')
  47. ->setDescription('manage logging configuration')
  48. ->addOption(
  49. 'backend',
  50. null,
  51. InputOption::VALUE_REQUIRED,
  52. 'set the logging backend [file, syslog, errorlog, systemd]'
  53. )
  54. ->addOption(
  55. 'level',
  56. null,
  57. InputOption::VALUE_REQUIRED,
  58. 'set the log level [debug, info, warning, error, fatal]'
  59. )
  60. ->addOption(
  61. 'timezone',
  62. null,
  63. InputOption::VALUE_REQUIRED,
  64. 'set the logging timezone'
  65. )
  66. ;
  67. }
  68. protected function execute(InputInterface $input, OutputInterface $output): int {
  69. // collate config setting to the end, to avoid partial configuration
  70. $toBeSet = [];
  71. if ($backend = $input->getOption('backend')) {
  72. $this->validateBackend($backend);
  73. $toBeSet['log_type'] = $backend;
  74. }
  75. $level = $input->getOption('level');
  76. if ($level !== null) {
  77. if (is_numeric($level)) {
  78. $levelNum = $level;
  79. // sanity check
  80. $this->convertLevelNumber($levelNum);
  81. } else {
  82. $levelNum = $this->convertLevelString($level);
  83. }
  84. $toBeSet['loglevel'] = $levelNum;
  85. }
  86. if ($timezone = $input->getOption('timezone')) {
  87. $this->validateTimezone($timezone);
  88. $toBeSet['logtimezone'] = $timezone;
  89. }
  90. // set config
  91. foreach ($toBeSet as $option => $value) {
  92. $this->config->setSystemValue($option, $value);
  93. }
  94. // display configuration
  95. $backend = $this->config->getSystemValue('log_type', self::DEFAULT_BACKEND);
  96. $output->writeln('Enabled logging backend: '.$backend);
  97. $levelNum = $this->config->getSystemValue('loglevel', self::DEFAULT_LOG_LEVEL);
  98. $level = $this->convertLevelNumber($levelNum);
  99. $output->writeln('Log level: '.$level.' ('.$levelNum.')');
  100. $timezone = $this->config->getSystemValue('logtimezone', self::DEFAULT_TIMEZONE);
  101. $output->writeln('Log timezone: '.$timezone);
  102. return 0;
  103. }
  104. /**
  105. * @param string $backend
  106. * @throws \InvalidArgumentException
  107. */
  108. protected function validateBackend($backend) {
  109. if (!class_exists('OC\\Log\\'.ucfirst($backend))) {
  110. throw new \InvalidArgumentException('Invalid backend');
  111. }
  112. }
  113. /**
  114. * @param string $timezone
  115. * @throws \Exception
  116. */
  117. protected function validateTimezone($timezone) {
  118. new \DateTimeZone($timezone);
  119. }
  120. /**
  121. * @param string $level
  122. * @return int
  123. * @throws \InvalidArgumentException
  124. */
  125. protected function convertLevelString($level) {
  126. $level = strtolower($level);
  127. switch ($level) {
  128. case 'debug':
  129. return 0;
  130. case 'info':
  131. return 1;
  132. case 'warning':
  133. case 'warn':
  134. return 2;
  135. case 'error':
  136. case 'err':
  137. return 3;
  138. case 'fatal':
  139. return 4;
  140. }
  141. throw new \InvalidArgumentException('Invalid log level string');
  142. }
  143. /**
  144. * @param int $levelNum
  145. * @return string
  146. * @throws \InvalidArgumentException
  147. */
  148. protected function convertLevelNumber($levelNum) {
  149. switch ($levelNum) {
  150. case 0:
  151. return 'Debug';
  152. case 1:
  153. return 'Info';
  154. case 2:
  155. return 'Warning';
  156. case 3:
  157. return 'Error';
  158. case 4:
  159. return 'Fatal';
  160. }
  161. throw new \InvalidArgumentException('Invalid log level number');
  162. }
  163. /**
  164. * @param string $optionName
  165. * @param CompletionContext $context
  166. * @return string[]
  167. */
  168. public function completeOptionValues($optionName, CompletionContext $context) {
  169. if ($optionName === 'backend') {
  170. return ['file', 'syslog', 'errorlog', 'systemd'];
  171. } elseif ($optionName === 'level') {
  172. return ['debug', 'info', 'warning', 'error', 'fatal'];
  173. } elseif ($optionName === 'timezone') {
  174. return \DateTimeZone::listIdentifiers();
  175. }
  176. return [];
  177. }
  178. /**
  179. * @param string $argumentName
  180. * @param CompletionContext $context
  181. * @return string[]
  182. */
  183. public function completeArgumentValues($argumentName, CompletionContext $context) {
  184. return [];
  185. }
  186. }