Completed
Push — master ( c455d3...f8f428 )
by Jörn Friedrich
09:22
created

Syslog::write()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 28
Code Lines 20

Duplication

Lines 5
Ratio 17.86 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 20
c 1
b 0
f 0
nc 12
nop 3
dl 5
loc 28
rs 8.439
1
<?php
2
/**
3
 * @author Bart Visscher <[email protected]>
4
 * @author Morris Jobke <[email protected]>
5
 * @author Roeland Jago Douma <[email protected]>
6
 * @author Thomas Müller <[email protected]>
7
 *
8
 * @copyright Copyright (c) 2018, ownCloud GmbH
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
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, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OC\Log;
26
27
class Syslog {
28
	static protected $levels = [
29
		\OCP\Util::DEBUG => LOG_DEBUG,
30
		\OCP\Util::INFO => LOG_INFO,
31
		\OCP\Util::WARN => LOG_WARNING,
32
		\OCP\Util::ERROR => LOG_ERR,
33
		\OCP\Util::FATAL => LOG_CRIT,
34
	];
35
36
	public static $DEFAULT_FORMAT = '[%reqId%][%remoteAddr%][%user%][%app%][%method%][%url%] %message%';
37
38
	/**
39
	 * Init class data
40
	 */
41
	public static function init() {
42
		openlog(\OC::$server->getSystemConfig()->getValue("syslog_tag", "ownCloud"), LOG_PID | LOG_CONS, LOG_USER);
43
		// Close at shutdown
44
		register_shutdown_function('closelog');
45
	}
46
47
	/**
48
	 * write a message in the log
49
	 * @param string $app
50
	 * @param string $message
51
	 * @param int $level
52
	 */
53
	public static function write($app, $message, $level) {
54
		$syslogLevel = self::$levels[$level];
55
56
		$request = \OC::$server->getRequest();
57 View Code Duplication
		if(\OC::$server->getConfig()->getSystemValue('installed', false)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
			$user = (\OC_User::getUser()) ? \OC_User::getUser() : '--';
59
		} else {
60
			$user = '--';
61
		}
62
63
		$entry = [
64
			'%reqId%' => $request->getId(),
65
			'%level%' => $level, // not needed in the default log line format, added by syslog itself
66
			'%remoteAddr%' => $request->getRemoteAddress(),
67
			'%user%' => $user,
68
			'%app%' => $app,
69
			'%method%' => is_string($request->getMethod()) ? $request->getMethod() : '--',
70
			'%url%' => ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--',
71
			'%message%' => $message
72
		];
73
74
		$syslogFormat = \OC::$server->getConfig()->getSystemValue(
75
			'log.syslog.format', self::$DEFAULT_FORMAT
76
		);
77
78
		$entryLine = str_ireplace(array_keys($entry), array_values($entry), $syslogFormat);
79
		syslog($syslogLevel, $entryLine);
80
	}
81
}
82