LogSettingsController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Georg Ehrke <georg@owncloud.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  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\Settings\Controller;
  28. use OCP\AppFramework\Controller;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\Http\JSONResponse;
  31. use OCP\AppFramework\Http\StreamResponse;
  32. use OCP\IL10N;
  33. use OCP\IRequest;
  34. use OCP\IConfig;
  35. /**
  36. * Class LogSettingsController
  37. *
  38. * @package OC\Settings\Controller
  39. */
  40. class LogSettingsController extends Controller {
  41. /**
  42. * @var \OCP\IConfig
  43. */
  44. private $config;
  45. /**
  46. * @var \OCP\IL10N
  47. */
  48. private $l10n;
  49. /**
  50. * @param string $appName
  51. * @param IRequest $request
  52. * @param IConfig $config
  53. */
  54. public function __construct($appName,
  55. IRequest $request,
  56. IConfig $config,
  57. IL10N $l10n) {
  58. parent::__construct($appName, $request);
  59. $this->config = $config;
  60. $this->l10n = $l10n;
  61. }
  62. /**
  63. * set log level for logger
  64. *
  65. * @param int $level
  66. * @return JSONResponse
  67. */
  68. public function setLogLevel($level) {
  69. if ($level < 0 || $level > 4) {
  70. return new JSONResponse([
  71. 'message' => (string) $this->l10n->t('log-level out of allowed range'),
  72. ], Http::STATUS_BAD_REQUEST);
  73. }
  74. $this->config->setSystemValue('loglevel', $level);
  75. return new JSONResponse([
  76. 'level' => $level,
  77. ]);
  78. }
  79. /**
  80. * get log entries from logfile
  81. *
  82. * @param int $count
  83. * @param int $offset
  84. * @return JSONResponse
  85. */
  86. public function getEntries($count=50, $offset=0) {
  87. return new JSONResponse([
  88. 'data' => \OC\Log\File::getEntries($count, $offset),
  89. 'remain' => count(\OC\Log\File::getEntries(1, $offset + $count)) !== 0,
  90. ]);
  91. }
  92. /**
  93. * download logfile
  94. *
  95. * @NoCSRFRequired
  96. *
  97. * @return StreamResponse
  98. */
  99. public function download() {
  100. $resp = new StreamResponse(\OC\Log\File::getLogFilePath());
  101. $resp->addHeader('Content-Type', 'application/octet-stream');
  102. $resp->addHeader('Content-Disposition', 'attachment; filename="nextcloud.log"');
  103. return $resp;
  104. }
  105. }