Passed
Push — master ( b1ca74...11e228 )
by Christoph
60:05 queued 47:35
created

LogFactory   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCustomPsrLogger() 0 4 1
B get() 0 16 7
A __construct() 0 3 1
A getCustomLogger() 0 3 1
A buildLogFile() 0 8 3
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Johannes Ernst <[email protected]>
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
26
namespace OC\Log;
27
28
use OC\Log;
29
use OC\SystemConfig;
30
use OCP\ILogger;
31
use OCP\IServerContainer;
32
use OCP\Log\ILogFactory;
33
use OCP\Log\IWriter;
34
use Psr\Log\LoggerInterface;
35
36
class LogFactory implements ILogFactory {
37
	/** @var IServerContainer */
38
	private $c;
39
	/** @var SystemConfig */
40
	private $systemConfig;
41
42
	public function __construct(IServerContainer $c, SystemConfig $systemConfig) {
43
		$this->c = $c;
44
		$this->systemConfig = $systemConfig;
45
	}
46
47
	/**
48
	 * @throws \OCP\AppFramework\QueryException
49
	 */
50
	public function get(string $type):IWriter {
51
		switch (strtolower($type)) {
52
			case 'errorlog':
53
				return new Errorlog();
54
			case 'syslog':
55
				return $this->c->resolve(Syslog::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->c->resolve(OC\Log\Syslog::class) returns the type stdClass which is incompatible with the type-hinted return OCP\Log\IWriter.
Loading history...
56
			case 'systemd':
57
				return $this->c->resolve(Systemdlog::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->c->resolve...\Log\Systemdlog::class) returns the type stdClass which is incompatible with the type-hinted return OCP\Log\IWriter.
Loading history...
58
			case 'file':
59
				return $this->buildLogFile();
60
61
			// Backwards compatibility for old and fallback for unknown log types
62
			case 'owncloud':
63
			case 'nextcloud':
64
			default:
65
				return $this->buildLogFile();
66
		}
67
	}
68
69
	public function getCustomLogger(string $path):ILogger {
70
		$log = $this->buildLogFile($path);
71
		return new Log($log, $this->systemConfig);
72
	}
73
74
	public function getCustomPsrLogger(string $path): LoggerInterface {
75
		$log = $this->buildLogFile($path);
76
		return new PsrLoggerAdapter(
77
			new Log($log, $this->systemConfig)
78
		);
79
	}
80
81
	protected function buildLogFile(string $logFile = ''):File {
82
		$defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log';
83
		if ($logFile === '') {
84
			$logFile = $this->systemConfig->getValue('logfile', $defaultLogFile);
85
		}
86
		$fallback = $defaultLogFile !== $logFile ? $defaultLogFile : '';
87
88
		return new File($logFile, $fallback, $this->systemConfig);
89
	}
90
}
91