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.

LogFactory.php 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. use Psr\Log\LoggerInterface;
  33. class LogFactory implements ILogFactory {
  34. public function __construct(
  35. private IServerContainer $c,
  36. private SystemConfig $systemConfig,
  37. ) {
  38. }
  39. /**
  40. * @throws \OCP\AppFramework\QueryException
  41. */
  42. public function get(string $type):IWriter {
  43. return match (strtolower($type)) {
  44. 'errorlog' => new Errorlog($this->systemConfig),
  45. 'syslog' => $this->c->resolve(Syslog::class),
  46. 'systemd' => $this->c->resolve(Systemdlog::class),
  47. 'file' => $this->buildLogFile(),
  48. default => $this->buildLogFile(),
  49. };
  50. }
  51. public function getCustomLogger(string $path): ILogger {
  52. $log = $this->buildLogFile($path);
  53. return new Log($log, $this->systemConfig);
  54. }
  55. protected function createNewLogger(string $type, string $tag, string $path): IWriter {
  56. return match (strtolower($type)) {
  57. 'errorlog' => new Errorlog($this->systemConfig, $tag),
  58. 'syslog' => new Syslog($this->systemConfig, $tag),
  59. 'systemd' => new Systemdlog($this->systemConfig, $tag),
  60. default => $this->buildLogFile($path),
  61. };
  62. }
  63. public function getCustomPsrLogger(string $path, string $type = 'file', string $tag = 'Nextcloud'): LoggerInterface {
  64. $log = $this->createNewLogger($type, $tag, $path);
  65. return new PsrLoggerAdapter(
  66. new Log($log, $this->systemConfig)
  67. );
  68. }
  69. protected function buildLogFile(string $logFile = ''): File {
  70. $defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log';
  71. if ($logFile === '') {
  72. $logFile = $this->systemConfig->getValue('logfile', $defaultLogFile);
  73. }
  74. $fallback = $defaultLogFile !== $logFile ? $defaultLogFile : '';
  75. return new File($logFile, $fallback, $this->systemConfig);
  76. }
  77. }