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.

82 lines
2.4 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Johannes Ernst <jernst@indiecomputing.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  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
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Log;
  26. use OC\Log;
  27. use OC\SystemConfig;
  28. use OCP\ILogger;
  29. use OCP\IServerContainer;
  30. use OCP\Log\ILogFactory;
  31. use OCP\Log\IWriter;
  32. class LogFactory implements ILogFactory {
  33. /** @var IServerContainer */
  34. private $c;
  35. /** @var SystemConfig */
  36. private $systemConfig;
  37. public function __construct(IServerContainer $c, SystemConfig $systemConfig) {
  38. $this->c = $c;
  39. $this->systemConfig = $systemConfig;
  40. }
  41. /**
  42. * @throws \OCP\AppFramework\QueryException
  43. */
  44. public function get(string $type):IWriter {
  45. switch (strtolower($type)) {
  46. case 'errorlog':
  47. return new Errorlog();
  48. case 'syslog':
  49. return $this->c->resolve(Syslog::class);
  50. case 'systemd':
  51. return $this->c->resolve(Systemdlog::class);
  52. case 'file':
  53. return $this->buildLogFile();
  54. // Backwards compatibility for old and fallback for unknown log types
  55. case 'owncloud':
  56. case 'nextcloud':
  57. default:
  58. return $this->buildLogFile();
  59. }
  60. }
  61. public function getCustomLogger(string $path):ILogger {
  62. $log = $this->buildLogFile($path);
  63. return new Log($log, $this->systemConfig);
  64. }
  65. protected function buildLogFile(string $logFile = ''):File {
  66. $defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log';
  67. if ($logFile === '') {
  68. $logFile = $this->systemConfig->getValue('logfile', $defaultLogFile);
  69. }
  70. $fallback = $defaultLogFile !== $logFile ? $defaultLogFile : '';
  71. return new File($logFile, $fallback, $this->systemConfig);
  72. }
  73. }