Manage.php 5.2 KB

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