Manage.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /** @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): int {
  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. return 0;
  104. }
  105. /**
  106. * @param string $backend
  107. * @throws \InvalidArgumentException
  108. */
  109. protected function validateBackend($backend) {
  110. if (!class_exists('OC\\Log\\'.ucfirst($backend))) {
  111. throw new \InvalidArgumentException('Invalid backend');
  112. }
  113. }
  114. /**
  115. * @param string $timezone
  116. * @throws \Exception
  117. */
  118. protected function validateTimezone($timezone) {
  119. new \DateTimeZone($timezone);
  120. }
  121. /**
  122. * @param string $level
  123. * @return int
  124. * @throws \InvalidArgumentException
  125. */
  126. protected function convertLevelString($level) {
  127. $level = strtolower($level);
  128. switch ($level) {
  129. case 'debug':
  130. return 0;
  131. case 'info':
  132. return 1;
  133. case 'warning':
  134. case 'warn':
  135. return 2;
  136. case 'error':
  137. case 'err':
  138. return 3;
  139. case 'fatal':
  140. return 4;
  141. }
  142. throw new \InvalidArgumentException('Invalid log level string');
  143. }
  144. /**
  145. * @param int $levelNum
  146. * @return string
  147. * @throws \InvalidArgumentException
  148. */
  149. protected function convertLevelNumber($levelNum) {
  150. switch ($levelNum) {
  151. case 0:
  152. return 'Debug';
  153. case 1:
  154. return 'Info';
  155. case 2:
  156. return 'Warning';
  157. case 3:
  158. return 'Error';
  159. case 4:
  160. return 'Fatal';
  161. }
  162. throw new \InvalidArgumentException('Invalid log level number');
  163. }
  164. /**
  165. * @param string $optionName
  166. * @param CompletionContext $context
  167. * @return string[]
  168. */
  169. public function completeOptionValues($optionName, CompletionContext $context) {
  170. if ($optionName === 'backend') {
  171. return ['file', 'syslog', 'errorlog', 'systemd'];
  172. } elseif ($optionName === 'level') {
  173. return ['debug', 'info', 'warning', 'error', 'fatal'];
  174. } elseif ($optionName === 'timezone') {
  175. $identifier = \DateTimeZone::listIdentifiers();
  176. if ($identifier === false) {
  177. return [];
  178. }
  179. return $identifier;
  180. }
  181. return [];
  182. }
  183. /**
  184. * @param string $argumentName
  185. * @param CompletionContext $context
  186. * @return string[]
  187. */
  188. public function completeArgumentValues($argumentName, CompletionContext $context) {
  189. return [];
  190. }
  191. }