You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Manage.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Robin McCorkell <robin@mccorkell.me.uk>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Pulzer <t.pulzer@kniel.de>
  9. * @author Johannes Ernst <jernst@indiecomputing.com>
  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\CompletionAwareInterface;
  29. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  30. use Symfony\Component\Console\Command\Command;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Input\InputOption;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. class Manage extends Command implements CompletionAwareInterface {
  35. const DEFAULT_BACKEND = 'file';
  36. const DEFAULT_LOG_LEVEL = 2;
  37. const DEFAULT_TIMEZONE = 'UTC';
  38. /** @var IConfig */
  39. protected $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]'
  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) {
  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. }
  103. /**
  104. * @param string $backend
  105. * @throws \InvalidArgumentException
  106. */
  107. protected function validateBackend($backend) {
  108. if (!class_exists('OC\\Log\\'.ucfirst($backend))) {
  109. throw new \InvalidArgumentException('Invalid backend');
  110. }
  111. }
  112. /**
  113. * @param string $timezone
  114. * @throws \Exception
  115. */
  116. protected function validateTimezone($timezone) {
  117. new \DateTimeZone($timezone);
  118. }
  119. /**
  120. * @param string $level
  121. * @return int
  122. * @throws \InvalidArgumentException
  123. */
  124. protected function convertLevelString($level) {
  125. $level = strtolower($level);
  126. switch ($level) {
  127. case 'debug':
  128. return 0;
  129. case 'info':
  130. return 1;
  131. case 'warning':
  132. case 'warn':
  133. return 2;
  134. case 'error':
  135. case 'err':
  136. return 3;
  137. }
  138. throw new \InvalidArgumentException('Invalid log level string');
  139. }
  140. /**
  141. * @param int $levelNum
  142. * @return string
  143. * @throws \InvalidArgumentException
  144. */
  145. protected function convertLevelNumber($levelNum) {
  146. switch ($levelNum) {
  147. case 0:
  148. return 'Debug';
  149. case 1:
  150. return 'Info';
  151. case 2:
  152. return 'Warning';
  153. case 3:
  154. return 'Error';
  155. }
  156. throw new \InvalidArgumentException('Invalid log level number');
  157. }
  158. /**
  159. * @param string $optionName
  160. * @param CompletionContext $context
  161. * @return string[]
  162. */
  163. public function completeOptionValues($optionName, CompletionContext $context) {
  164. if ($optionName === 'backend') {
  165. return ['file', 'syslog', 'errorlog', 'systemd'];
  166. } else if ($optionName === 'level') {
  167. return ['debug', 'info', 'warning', 'error'];
  168. } else if ($optionName === 'timezone') {
  169. return \DateTimeZone::listIdentifiers();
  170. }
  171. return [];
  172. }
  173. /**
  174. * @param string $argumentName
  175. * @param CompletionContext $context
  176. * @return string[]
  177. */
  178. public function completeArgumentValues($argumentName, CompletionContext $context) {
  179. return [];
  180. }
  181. }