Completed
Push — master ( a56ec1...2aa108 )
by Morris
44:44 queued 16:07
created

LogFactory::get()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 6
nop 1
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
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
24
namespace OC\Log;
25
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
33
class LogFactory implements ILogFactory {
34
	/** @var IServerContainer */
35
	private $c;
36
	/** @var SystemConfig */
37
	private $systemConfig;
38
39
	public function __construct(IServerContainer $c, SystemConfig $systemConfig) {
40
		$this->c = $c;
41
		$this->systemConfig = $systemConfig;
42
	}
43
44
	/**
45
	 * @throws \OCP\AppFramework\QueryException
46
	 */
47
	public function get(string $type):IWriter {
48
		switch (strtolower($type)) {
49
			case 'errorlog':
50
				return new Errorlog();
51
			case 'syslog':
52
				return $this->c->resolve(Syslog::class);
53
			case 'file':
54
				return $this->buildLogFile();
55
56
			// Backwards compatibility for old and fallback for unknown log types
57
			case 'owncloud':
58
			case 'nextcloud':
59
			default:
60
				return $this->buildLogFile();
61
		}
62
	}
63
64
	public function getCustomLogger(string $path):ILogger {
65
		$log = $this->buildLogFile($path);
66
		return 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
76
		return new File($logFile, $fallback, $this->systemConfig);
77
	}
78
}
79