LogFactory.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Log;
  7. use OC\Log;
  8. use OC\SystemConfig;
  9. use OCP\IServerContainer;
  10. use OCP\Log\ILogFactory;
  11. use OCP\Log\IWriter;
  12. use Psr\Log\LoggerInterface;
  13. class LogFactory implements ILogFactory {
  14. public function __construct(
  15. private IServerContainer $c,
  16. private SystemConfig $systemConfig,
  17. ) {
  18. }
  19. /**
  20. * @throws \OCP\AppFramework\QueryException
  21. */
  22. public function get(string $type):IWriter {
  23. return match (strtolower($type)) {
  24. 'errorlog' => new Errorlog($this->systemConfig),
  25. 'syslog' => $this->c->resolve(Syslog::class),
  26. 'systemd' => $this->c->resolve(Systemdlog::class),
  27. 'file' => $this->buildLogFile(),
  28. default => $this->buildLogFile(),
  29. };
  30. }
  31. protected function createNewLogger(string $type, string $tag, string $path): IWriter {
  32. return match (strtolower($type)) {
  33. 'errorlog' => new Errorlog($this->systemConfig, $tag),
  34. 'syslog' => new Syslog($this->systemConfig, $tag),
  35. 'systemd' => new Systemdlog($this->systemConfig, $tag),
  36. default => $this->buildLogFile($path),
  37. };
  38. }
  39. public function getCustomPsrLogger(string $path, string $type = 'file', string $tag = 'Nextcloud'): LoggerInterface {
  40. $log = $this->createNewLogger($type, $tag, $path);
  41. return new PsrLoggerAdapter(
  42. new Log($log, $this->systemConfig)
  43. );
  44. }
  45. protected function buildLogFile(string $logFile = ''): File {
  46. $defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT . '/data') . '/nextcloud.log';
  47. if ($logFile === '') {
  48. $logFile = $this->systemConfig->getValue('logfile', $defaultLogFile);
  49. }
  50. $fallback = $defaultLogFile !== $logFile ? $defaultLogFile : '';
  51. return new File($logFile, $fallback, $this->systemConfig);
  52. }
  53. }