LogFactory.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 Johannes Ernst <jernst@indiecomputing.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Log;
  25. use OC\Log;
  26. use OC\SystemConfig;
  27. use OCP\ILogger;
  28. use OCP\IServerContainer;
  29. use OCP\Log\ILogFactory;
  30. use OCP\Log\IWriter;
  31. class LogFactory implements ILogFactory {
  32. /** @var IServerContainer */
  33. private $c;
  34. /** @var SystemConfig */
  35. private $systemConfig;
  36. public function __construct(IServerContainer $c, SystemConfig $systemConfig) {
  37. $this->c = $c;
  38. $this->systemConfig = $systemConfig;
  39. }
  40. /**
  41. * @throws \OCP\AppFramework\QueryException
  42. */
  43. public function get(string $type):IWriter {
  44. switch (strtolower($type)) {
  45. case 'errorlog':
  46. return new Errorlog();
  47. case 'syslog':
  48. return $this->c->resolve(Syslog::class);
  49. case 'systemd':
  50. return $this->c->resolve(Systemdlog::class);
  51. case 'file':
  52. return $this->buildLogFile();
  53. // Backwards compatibility for old and fallback for unknown log types
  54. case 'owncloud':
  55. case 'nextcloud':
  56. default:
  57. return $this->buildLogFile();
  58. }
  59. }
  60. public function getCustomLogger(string $path):ILogger {
  61. $log = $this->buildLogFile($path);
  62. return new Log($log, $this->systemConfig);
  63. }
  64. protected function buildLogFile(string $logFile = ''):File {
  65. $defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log';
  66. if($logFile === '') {
  67. $logFile = $this->systemConfig->getValue('logfile', $defaultLogFile);
  68. }
  69. $fallback = $defaultLogFile !== $logFile ? $defaultLogFile : '';
  70. return new File($logFile, $fallback, $this->systemConfig);
  71. }
  72. }