Manage.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Johannes Ernst <jernst@indiecomputing.com>
  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. * @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. const DEFAULT_BACKEND = 'file';
  37. const DEFAULT_LOG_LEVEL = 2;
  38. const DEFAULT_TIMEZONE = 'UTC';
  39. /** @var IConfig */
  40. protected $config;
  41. public function __construct(IConfig $config) {
  42. $this->config = $config;
  43. parent::__construct();
  44. }
  45. protected function configure() {
  46. $this
  47. ->setName('log:manage')
  48. ->setDescription('manage logging configuration')
  49. ->addOption(
  50. 'backend',
  51. null,
  52. InputOption::VALUE_REQUIRED,
  53. 'set the logging backend [file, syslog, errorlog, systemd]'
  54. )
  55. ->addOption(
  56. 'level',
  57. null,
  58. InputOption::VALUE_REQUIRED,
  59. 'set the log level [debug, info, warning, error, fatal]'
  60. )
  61. ->addOption(
  62. 'timezone',
  63. null,
  64. InputOption::VALUE_REQUIRED,
  65. 'set the logging timezone'
  66. )
  67. ;
  68. }
  69. protected function execute(InputInterface $input, OutputInterface $output) {
  70. // collate config setting to the end, to avoid partial configuration
  71. $toBeSet = [];
  72. if ($backend = $input->getOption('backend')) {
  73. $this->validateBackend($backend);
  74. $toBeSet['log_type'] = $backend;
  75. }
  76. $level = $input->getOption('level');
  77. if ($level !== null) {
  78. if (is_numeric($level)) {
  79. $levelNum = $level;
  80. // sanity check
  81. $this->convertLevelNumber($levelNum);
  82. } else {
  83. $levelNum = $this->convertLevelString($level);
  84. }
  85. $toBeSet['loglevel'] = $levelNum;
  86. }
  87. if ($timezone = $input->getOption('timezone')) {
  88. $this->validateTimezone($timezone);
  89. $toBeSet['logtimezone'] = $timezone;
  90. }
  91. // set config
  92. foreach ($toBeSet as $option => $value) {
  93. $this->config->setSystemValue($option, $value);
  94. }
  95. // display configuration
  96. $backend = $this->config->getSystemValue('log_type', self::DEFAULT_BACKEND);
  97. $output->writeln('Enabled logging backend: '.$backend);
  98. $levelNum = $this->config->getSystemValue('loglevel', self::DEFAULT_LOG_LEVEL);
  99. $level = $this->convertLevelNumber($levelNum);
  100. $output->writeln('Log level: '.$level.' ('.$levelNum.')');
  101. $timezone = $this->config->getSystemValue('logtimezone', self::DEFAULT_TIMEZONE);
  102. $output->writeln('Log timezone: '.$timezone);
  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. } else if ($optionName === 'level') {
  172. return ['debug', 'info', 'warning', 'error', 'fatal'];
  173. } else if ($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. }