LogFactory.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. /** @var IServerContainer */
  35. private $c;
  36. /** @var SystemConfig */
  37. private $systemConfig;
  38. public function __construct(IServerContainer $c, SystemConfig $systemConfig) {
  39. $this->c = $c;
  40. $this->systemConfig = $systemConfig;
  41. }
  42. /**
  43. * @throws \OCP\AppFramework\QueryException
  44. */
  45. public function get(string $type):IWriter {
  46. switch (strtolower($type)) {
  47. case 'errorlog':
  48. return new Errorlog($this->systemConfig);
  49. case 'syslog':
  50. return $this->c->resolve(Syslog::class);
  51. case 'systemd':
  52. return $this->c->resolve(Systemdlog::class);
  53. case 'file':
  54. return $this->buildLogFile();
  55. // Backwards compatibility for old and fallback for unknown log types
  56. case 'owncloud':
  57. case 'nextcloud':
  58. default:
  59. return $this->buildLogFile();
  60. }
  61. }
  62. public function getCustomLogger(string $path):ILogger {
  63. $log = $this->buildLogFile($path);
  64. return new Log($log, $this->systemConfig);
  65. }
  66. protected function createNewLogger(string $type, string $tag, string $path): IWriter {
  67. switch (strtolower($type)) {
  68. case 'errorlog':
  69. return new Errorlog($this->systemConfig, $tag);
  70. case 'syslog':
  71. return new Syslog($this->systemConfig, $tag);
  72. case 'systemd':
  73. return new Systemdlog($this->systemConfig, $tag);
  74. case 'file':
  75. case 'owncloud':
  76. case 'nextcloud':
  77. default:
  78. return $this->buildLogFile($path);
  79. }
  80. }
  81. public function getCustomPsrLogger(string $path, string $type = 'file', string $tag = 'Nextcloud'): LoggerInterface {
  82. $log = $this->createNewLogger($type, $tag, $path);
  83. return new PsrLoggerAdapter(
  84. new Log($log, $this->systemConfig)
  85. );
  86. }
  87. protected function buildLogFile(string $logFile = ''):File {
  88. $defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log';
  89. if ($logFile === '') {
  90. $logFile = $this->systemConfig->getValue('logfile', $defaultLogFile);
  91. }
  92. $fallback = $defaultLogFile !== $logFile ? $defaultLogFile : '';
  93. return new File($logFile, $fallback, $this->systemConfig);
  94. }
  95. }