Passed
Push — master ( 562c57...272f4c )
by John
13:44 queued 12s
created

LogFactory::createNewLogger()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 7
nop 3
dl 0
loc 13
rs 8.8333
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
 * @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
namespace OC\Log;
26
27
use OC\Log;
28
use OC\SystemConfig;
29
use OCP\ILogger;
30
use OCP\IServerContainer;
31
use OCP\Log\ILogFactory;
32
use OCP\Log\IWriter;
33
use Psr\Log\LoggerInterface;
34
35
class LogFactory implements ILogFactory {
36
	/** @var IServerContainer */
37
	private $c;
38
	/** @var SystemConfig */
39
	private $systemConfig;
40
41
	public function __construct(IServerContainer $c, SystemConfig $systemConfig) {
42
		$this->c = $c;
43
		$this->systemConfig = $systemConfig;
44
	}
45
46
	/**
47
	 * @throws \OCP\AppFramework\QueryException
48
	 */
49
	public function get(string $type):IWriter {
50
		switch (strtolower($type)) {
51
			case 'errorlog':
52
				return new Errorlog();
53
			case 'syslog':
54
				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...
55
			case 'systemd':
56
				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...
57
			case 'file':
58
				return $this->buildLogFile();
59
60
			// Backwards compatibility for old and fallback for unknown log types
61
			case 'owncloud':
62
			case 'nextcloud':
63
			default:
64
				return $this->buildLogFile();
65
		}
66
	}
67
68
	public function getCustomLogger(string $path):ILogger {
69
		$log = $this->buildLogFile($path);
70
		return new Log($log, $this->systemConfig);
71
	}
72
73
	protected function createNewLogger(string $type, string $tag, string $path): IWriter {
74
		switch (strtolower($type)) {
75
			case 'errorlog':
76
				return new Errorlog($tag);
77
			case 'syslog':
78
				return new Syslog($this->systemConfig, $tag);
79
			case 'systemd':
80
				return new Systemdlog($this->systemConfig, $tag);
81
			case 'file':
82
			case 'owncloud':
83
			case 'nextcloud':
84
			default:
85
				return $this->buildLogFile($path);
86
		}
87
	}
88
89
	public function getCustomPsrLogger(string $path, string $type = 'file', string $tag = 'Nextcloud'): LoggerInterface {
90
		$log = $this->createNewLogger($type, $tag, $path);
91
		return new PsrLoggerAdapter(
92
			new Log($log, $this->systemConfig)
93
		);
94
	}
95
96
	protected function buildLogFile(string $logFile = ''):File {
97
		$defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log';
98
		if ($logFile === '') {
99
			$logFile = $this->systemConfig->getValue('logfile', $defaultLogFile);
100
		}
101
		$fallback = $defaultLogFile !== $logFile ? $defaultLogFile : '';
102
103
		return new File($logFile, $fallback, $this->systemConfig);
104
	}
105
}
106