Passed
Push — master ( eb92c5...c0e123 )
by Morris
15:20 queued 12s
created

LogDetails::logDetails()   D

Complexity

Conditions 11
Paths 384

Size

Total Lines 61
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 45
c 1
b 0
f 0
nc 384
nop 3
dl 0
loc 61
rs 4.1833

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2019 Julius Härtl <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Julius Härtl <[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\SystemConfig;
28
29
abstract class LogDetails {
30
31
	/** @var SystemConfig */
32
	private $config;
33
34
	public function __construct(SystemConfig $config) {
35
		$this->config = $config;
36
	}
37
38
	public function logDetails(string $app, $message, int $level): array {
39
		// default to ISO8601
40
		$format = $this->config->getValue('logdateformat', \DateTime::ATOM);
41
		$logTimeZone = $this->config->getValue('logtimezone', 'UTC');
42
		try {
43
			$timezone = new \DateTimeZone($logTimeZone);
44
		} catch (\Exception $e) {
45
			$timezone = new \DateTimeZone('UTC');
46
		}
47
		$time = \DateTime::createFromFormat("U.u", number_format(microtime(true), 4, ".", ""));
48
		if ($time === false) {
49
			$time = new \DateTime(null, $timezone);
50
		} else {
51
			// apply timezone if $time is created from UNIX timestamp
52
			$time->setTimezone($timezone);
53
		}
54
		$request = \OC::$server->getRequest();
55
		$reqId = $request->getId();
56
		$remoteAddr = $request->getRemoteAddress();
57
		// remove username/passwords from URLs before writing the to the log file
58
		$time = $time->format($format);
59
		$url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--';
60
		$method = is_string($request->getMethod()) ? $request->getMethod() : '--';
0 ignored issues
show
introduced by
The condition is_string($request->getMethod()) is always true.
Loading history...
61
		if ($this->config->getValue('installed', false)) {
62
			$user = \OC_User::getUser() ? \OC_User::getUser() : '--';
63
		} else {
64
			$user = '--';
65
		}
66
		$userAgent = $request->getHeader('User-Agent');
67
		if ($userAgent === '') {
68
			$userAgent = '--';
69
		}
70
		$version = $this->config->getValue('version', '');
71
		$entry = compact(
72
			'reqId',
73
			'level',
74
			'time',
75
			'remoteAddr',
76
			'user',
77
			'app',
78
			'method',
79
			'url',
80
			'message',
81
			'userAgent',
82
			'version'
83
		);
84
85
		if (is_array($message)) {
86
			// Exception messages are extracted and the exception is put into a separate field
87
			// anything else modern is split to 'message' (string) and
88
			// data (array) fields
89
			if (array_key_exists('Exception', $message)) {
90
				$entry['exception'] = $message;
91
				$entry['message'] = $message['CustomMessage'] !== '--' ? $message['CustomMessage'] : $message['Message'];
92
			} else {
93
				$entry['data'] = $message;
94
				$entry['message'] = $message['message'] ?? '(no message provided)';
95
			}
96
		}
97
98
		return $entry;
99
	}
100
101
	public function logDetailsAsJSON(string $app, $message, int $level): string {
102
		$entry = $this->logDetails($app, $message, $level);
103
		// PHP's json_encode only accept proper UTF-8 strings, loop over all
104
		// elements to ensure that they are properly UTF-8 compliant or convert
105
		// them manually.
106
		foreach ($entry as $key => $value) {
107
			if (is_string($value)) {
108
				$testEncode = json_encode($value, JSON_UNESCAPED_SLASHES);
109
				if ($testEncode === false) {
110
					$entry[$key] = utf8_encode($value);
111
				}
112
			}
113
		}
114
		return json_encode($entry, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES);
115
	}
116
}
117