|
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() : '--'; |
|
|
|
|
|
|
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
|
|
|
|