1
0

LogFactory.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\ILogger;
  10. use OCP\IServerContainer;
  11. use OCP\Log\ILogFactory;
  12. use OCP\Log\IWriter;
  13. use Psr\Log\LoggerInterface;
  14. class LogFactory implements ILogFactory {
  15. public function __construct(
  16. private IServerContainer $c,
  17. private SystemConfig $systemConfig,
  18. ) {
  19. }
  20. /**
  21. * @throws \OCP\AppFramework\QueryException
  22. */
  23. public function get(string $type):IWriter {
  24. return match (strtolower($type)) {
  25. 'errorlog' => new Errorlog($this->systemConfig),
  26. 'syslog' => $this->c->resolve(Syslog::class),
  27. 'systemd' => $this->c->resolve(Systemdlog::class),
  28. 'file' => $this->buildLogFile(),
  29. default => $this->buildLogFile(),
  30. };
  31. }
  32. public function getCustomLogger(string $path): ILogger {
  33. $log = $this->buildLogFile($path);
  34. return new Log($log, $this->systemConfig);
  35. }
  36. protected function createNewLogger(string $type, string $tag, string $path): IWriter {
  37. return match (strtolower($type)) {
  38. 'errorlog' => new Errorlog($this->systemConfig, $tag),
  39. 'syslog' => new Syslog($this->systemConfig, $tag),
  40. 'systemd' => new Systemdlog($this->systemConfig, $tag),
  41. default => $this->buildLogFile($path),
  42. };
  43. }
  44. public function getCustomPsrLogger(string $path, string $type = 'file', string $tag = 'Nextcloud'): LoggerInterface {
  45. $log = $this->createNewLogger($type, $tag, $path);
  46. return new PsrLoggerAdapter(
  47. new Log($log, $this->systemConfig)
  48. );
  49. }
  50. protected function buildLogFile(string $logFile = ''): File {
  51. $defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log';
  52. if ($logFile === '') {
  53. $logFile = $this->systemConfig->getValue('logfile', $defaultLogFile);
  54. }
  55. $fallback = $defaultLogFile !== $logFile ? $defaultLogFile : '';
  56. return new File($logFile, $fallback, $this->systemConfig);
  57. }
  58. }