|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @copyright 2020 Christoph Wurst <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @author Christoph Wurst <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* @license GNU AGPL version 3 or any later version |
|
11
|
|
|
* |
|
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
15
|
|
|
* License, or (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU Affero General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
namespace OC\Log; |
|
27
|
|
|
|
|
28
|
|
|
use OC\Log; |
|
29
|
|
|
use OCP\EventDispatcher\IEventDispatcher; |
|
30
|
|
|
use OCP\ILogger; |
|
31
|
|
|
use OCP\Log\IDataLogger; |
|
32
|
|
|
use Psr\Log\InvalidArgumentException; |
|
33
|
|
|
use Psr\Log\LoggerInterface; |
|
34
|
|
|
use Throwable; |
|
35
|
|
|
use function array_key_exists; |
|
36
|
|
|
use function array_merge; |
|
37
|
|
|
|
|
38
|
|
|
final class PsrLoggerAdapter implements LoggerInterface, IDataLogger { |
|
39
|
|
|
/** @var Log */ |
|
40
|
|
|
private $logger; |
|
41
|
|
|
|
|
42
|
|
|
public function __construct(Log $logger) { |
|
43
|
|
|
$this->logger = $logger; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function setEventDispatcher(IEventDispatcher $eventDispatcher) { |
|
47
|
|
|
$this->logger->setEventDispatcher($eventDispatcher); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
private function containsThrowable(array $context): bool { |
|
51
|
|
|
return array_key_exists('exception', $context) && $context['exception'] instanceof Throwable; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* System is unusable. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $message |
|
58
|
|
|
* @param array $context |
|
59
|
|
|
* |
|
60
|
|
|
* @return void |
|
61
|
|
|
*/ |
|
62
|
|
|
public function emergency($message, array $context = []): void { |
|
63
|
|
|
if ($this->containsThrowable($context)) { |
|
64
|
|
|
$this->logger->logException($context['exception'], array_merge( |
|
65
|
|
|
[ |
|
66
|
|
|
'message' => $message, |
|
67
|
|
|
'level' => ILogger::FATAL, |
|
|
|
|
|
|
68
|
|
|
], |
|
69
|
|
|
$context |
|
70
|
|
|
)); |
|
71
|
|
|
} else { |
|
72
|
|
|
$this->logger->emergency($message, $context); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Action must be taken immediately. |
|
78
|
|
|
* |
|
79
|
|
|
* Example: Entire website down, database unavailable, etc. This should |
|
80
|
|
|
* trigger the SMS alerts and wake you up. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $message |
|
83
|
|
|
* @param array $context |
|
84
|
|
|
* |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
|
|
public function alert($message, array $context = []) { |
|
88
|
|
|
if ($this->containsThrowable($context)) { |
|
89
|
|
|
$this->logger->logException($context['exception'], array_merge( |
|
90
|
|
|
[ |
|
91
|
|
|
'message' => $message, |
|
92
|
|
|
'level' => ILogger::ERROR, |
|
|
|
|
|
|
93
|
|
|
], |
|
94
|
|
|
$context |
|
95
|
|
|
)); |
|
96
|
|
|
} else { |
|
97
|
|
|
$this->logger->alert($message, $context); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Critical conditions. |
|
103
|
|
|
* |
|
104
|
|
|
* Example: Application component unavailable, unexpected exception. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $message |
|
107
|
|
|
* @param array $context |
|
108
|
|
|
* |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
|
|
public function critical($message, array $context = []) { |
|
112
|
|
|
if ($this->containsThrowable($context)) { |
|
113
|
|
|
$this->logger->logException($context['exception'], array_merge( |
|
114
|
|
|
[ |
|
115
|
|
|
'message' => $message, |
|
116
|
|
|
'level' => ILogger::ERROR, |
|
|
|
|
|
|
117
|
|
|
], |
|
118
|
|
|
$context |
|
119
|
|
|
)); |
|
120
|
|
|
} else { |
|
121
|
|
|
$this->logger->critical($message, $context); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Runtime errors that do not require immediate action but should typically |
|
127
|
|
|
* be logged and monitored. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $message |
|
130
|
|
|
* @param array $context |
|
131
|
|
|
* |
|
132
|
|
|
* @return void |
|
133
|
|
|
*/ |
|
134
|
|
|
public function error($message, array $context = []) { |
|
135
|
|
|
if ($this->containsThrowable($context)) { |
|
136
|
|
|
$this->logger->logException($context['exception'], array_merge( |
|
137
|
|
|
[ |
|
138
|
|
|
'message' => $message, |
|
139
|
|
|
'level' => ILogger::ERROR, |
|
|
|
|
|
|
140
|
|
|
], |
|
141
|
|
|
$context |
|
142
|
|
|
)); |
|
143
|
|
|
} else { |
|
144
|
|
|
$this->logger->error($message, $context); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Exceptional occurrences that are not errors. |
|
150
|
|
|
* |
|
151
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
152
|
|
|
* that are not necessarily wrong. |
|
153
|
|
|
* |
|
154
|
|
|
* @param string $message |
|
155
|
|
|
* @param array $context |
|
156
|
|
|
* |
|
157
|
|
|
* @return void |
|
158
|
|
|
*/ |
|
159
|
|
|
public function warning($message, array $context = []) { |
|
160
|
|
|
if ($this->containsThrowable($context)) { |
|
161
|
|
|
$this->logger->logException($context['exception'], array_merge( |
|
162
|
|
|
[ |
|
163
|
|
|
'message' => $message, |
|
164
|
|
|
'level' => ILogger::WARN, |
|
|
|
|
|
|
165
|
|
|
], |
|
166
|
|
|
$context |
|
167
|
|
|
)); |
|
168
|
|
|
} else { |
|
169
|
|
|
$this->logger->warning($message, $context); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Normal but significant events. |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $message |
|
177
|
|
|
* @param array $context |
|
178
|
|
|
* |
|
179
|
|
|
* @return void |
|
180
|
|
|
*/ |
|
181
|
|
|
public function notice($message, array $context = []) { |
|
182
|
|
|
if ($this->containsThrowable($context)) { |
|
183
|
|
|
$this->logger->logException($context['exception'], array_merge( |
|
184
|
|
|
[ |
|
185
|
|
|
'message' => $message, |
|
186
|
|
|
'level' => ILogger::INFO, |
|
|
|
|
|
|
187
|
|
|
], |
|
188
|
|
|
$context |
|
189
|
|
|
)); |
|
190
|
|
|
} else { |
|
191
|
|
|
$this->logger->notice($message, $context); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Interesting events. |
|
197
|
|
|
* |
|
198
|
|
|
* Example: User logs in, SQL logs. |
|
199
|
|
|
* |
|
200
|
|
|
* @param string $message |
|
201
|
|
|
* @param array $context |
|
202
|
|
|
* |
|
203
|
|
|
* @return void |
|
204
|
|
|
*/ |
|
205
|
|
|
public function info($message, array $context = []) { |
|
206
|
|
|
if ($this->containsThrowable($context)) { |
|
207
|
|
|
$this->logger->logException($context['exception'], array_merge( |
|
208
|
|
|
[ |
|
209
|
|
|
'message' => $message, |
|
210
|
|
|
'level' => ILogger::INFO, |
|
|
|
|
|
|
211
|
|
|
], |
|
212
|
|
|
$context |
|
213
|
|
|
)); |
|
214
|
|
|
} else { |
|
215
|
|
|
$this->logger->info($message, $context); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Detailed debug information. |
|
221
|
|
|
* |
|
222
|
|
|
* @param string $message |
|
223
|
|
|
* @param array $context |
|
224
|
|
|
* |
|
225
|
|
|
* @return void |
|
226
|
|
|
*/ |
|
227
|
|
|
public function debug($message, array $context = []) { |
|
228
|
|
|
if ($this->containsThrowable($context)) { |
|
229
|
|
|
$this->logger->logException($context['exception'], array_merge( |
|
230
|
|
|
[ |
|
231
|
|
|
'message' => $message, |
|
232
|
|
|
'level' => ILogger::DEBUG, |
|
|
|
|
|
|
233
|
|
|
], |
|
234
|
|
|
$context |
|
235
|
|
|
)); |
|
236
|
|
|
} else { |
|
237
|
|
|
$this->logger->debug($message, $context); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Logs with an arbitrary level. |
|
243
|
|
|
* |
|
244
|
|
|
* @param mixed $level |
|
245
|
|
|
* @param string $message |
|
246
|
|
|
* @param array $context |
|
247
|
|
|
* |
|
248
|
|
|
* @return void |
|
249
|
|
|
* |
|
250
|
|
|
* @throws InvalidArgumentException |
|
251
|
|
|
*/ |
|
252
|
|
|
public function log($level, $message, array $context = []) { |
|
253
|
|
|
if (!is_int($level) || $level < ILogger::DEBUG || $level > ILogger::FATAL) { |
|
|
|
|
|
|
254
|
|
|
throw new InvalidArgumentException('Nextcloud allows only integer log levels'); |
|
255
|
|
|
} |
|
256
|
|
|
if ($this->containsThrowable($context)) { |
|
257
|
|
|
$this->logger->logException($context['exception'], array_merge( |
|
258
|
|
|
[ |
|
259
|
|
|
'message' => $message, |
|
260
|
|
|
'level' => $level, |
|
261
|
|
|
], |
|
262
|
|
|
$context |
|
263
|
|
|
)); |
|
264
|
|
|
} else { |
|
265
|
|
|
$this->logger->log($level, $message, $context); |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
public function logData(string $message, array $data, array $context = []): void { |
|
270
|
|
|
$this->logger->logData($message, $data, $context); |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
|
This class constant has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.