LogFactory.php 2.2 KB

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