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
|
|
|
|
27
|
|
|
namespace OC\Log; |
28
|
|
|
|
29
|
|
|
use OCP\ILogger; |
30
|
|
|
use Psr\Log\InvalidArgumentException; |
31
|
|
|
use Psr\Log\LoggerInterface; |
32
|
|
|
use Throwable; |
33
|
|
|
use function array_key_exists; |
34
|
|
|
use function array_merge; |
35
|
|
|
|
36
|
|
|
final class PsrLoggerAdapter implements LoggerInterface { |
37
|
|
|
|
38
|
|
|
/** @var ILogger */ |
39
|
|
|
private $logger; |
40
|
|
|
|
41
|
|
|
public function __construct(ILogger $logger) { |
42
|
|
|
$this->logger = $logger; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function containsThrowable(array $context): bool { |
46
|
|
|
return array_key_exists('exception', $context) && $context['exception'] instanceof Throwable; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* System is unusable. |
51
|
|
|
* |
52
|
|
|
* @param string $message |
53
|
|
|
* @param array $context |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function emergency($message, array $context = []): void { |
58
|
|
|
if ($this->containsThrowable($context)) { |
59
|
|
|
$this->logger->logException($context['exception'], array_merge( |
60
|
|
|
[ |
61
|
|
|
'message' => $message, |
62
|
|
|
'level' => ILogger::FATAL, |
63
|
|
|
], |
64
|
|
|
$context |
65
|
|
|
)); |
66
|
|
|
} else { |
67
|
|
|
$this->logger->emergency($message, $context); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Action must be taken immediately. |
73
|
|
|
* |
74
|
|
|
* Example: Entire website down, database unavailable, etc. This should |
75
|
|
|
* trigger the SMS alerts and wake you up. |
76
|
|
|
* |
77
|
|
|
* @param string $message |
78
|
|
|
* @param array $context |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function alert($message, array $context = []) { |
83
|
|
|
if ($this->containsThrowable($context)) { |
84
|
|
|
$this->logger->logException($context['exception'], array_merge( |
85
|
|
|
[ |
86
|
|
|
'message' => $message, |
87
|
|
|
'level' => ILogger::ERROR, |
88
|
|
|
], |
89
|
|
|
$context |
90
|
|
|
)); |
91
|
|
|
} else { |
92
|
|
|
$this->logger->alert($message, $context); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Critical conditions. |
98
|
|
|
* |
99
|
|
|
* Example: Application component unavailable, unexpected exception. |
100
|
|
|
* |
101
|
|
|
* @param string $message |
102
|
|
|
* @param array $context |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
public function critical($message, array $context = []) { |
107
|
|
|
if ($this->containsThrowable($context)) { |
108
|
|
|
$this->logger->logException($context['exception'], array_merge( |
109
|
|
|
[ |
110
|
|
|
'message' => $message, |
111
|
|
|
'level' => ILogger::ERROR, |
112
|
|
|
], |
113
|
|
|
$context |
114
|
|
|
)); |
115
|
|
|
} else { |
116
|
|
|
$this->logger->critical($message, $context); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Runtime errors that do not require immediate action but should typically |
122
|
|
|
* be logged and monitored. |
123
|
|
|
* |
124
|
|
|
* @param string $message |
125
|
|
|
* @param array $context |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function error($message, array $context = []) { |
130
|
|
|
if ($this->containsThrowable($context)) { |
131
|
|
|
$this->logger->logException($context['exception'], array_merge( |
132
|
|
|
[ |
133
|
|
|
'message' => $message, |
134
|
|
|
'level' => ILogger::ERROR, |
135
|
|
|
], |
136
|
|
|
$context |
137
|
|
|
)); |
138
|
|
|
} else { |
139
|
|
|
$this->logger->error($message, $context); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Exceptional occurrences that are not errors. |
145
|
|
|
* |
146
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things |
147
|
|
|
* that are not necessarily wrong. |
148
|
|
|
* |
149
|
|
|
* @param string $message |
150
|
|
|
* @param array $context |
151
|
|
|
* |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
public function warning($message, array $context = []) { |
155
|
|
|
if ($this->containsThrowable($context)) { |
156
|
|
|
$this->logger->logException($context['exception'], array_merge( |
157
|
|
|
[ |
158
|
|
|
'message' => $message, |
159
|
|
|
'level' => ILogger::WARN, |
160
|
|
|
], |
161
|
|
|
$context |
162
|
|
|
)); |
163
|
|
|
} else { |
164
|
|
|
$this->logger->warning($message, $context); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Normal but significant events. |
170
|
|
|
* |
171
|
|
|
* @param string $message |
172
|
|
|
* @param array $context |
173
|
|
|
* |
174
|
|
|
* @return void |
175
|
|
|
*/ |
176
|
|
|
public function notice($message, array $context = []) { |
177
|
|
|
if ($this->containsThrowable($context)) { |
178
|
|
|
$this->logger->logException($context['exception'], array_merge( |
179
|
|
|
[ |
180
|
|
|
'message' => $message, |
181
|
|
|
'level' => ILogger::INFO, |
182
|
|
|
], |
183
|
|
|
$context |
184
|
|
|
)); |
185
|
|
|
} else { |
186
|
|
|
$this->logger->notice($message, $context); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Interesting events. |
192
|
|
|
* |
193
|
|
|
* Example: User logs in, SQL logs. |
194
|
|
|
* |
195
|
|
|
* @param string $message |
196
|
|
|
* @param array $context |
197
|
|
|
* |
198
|
|
|
* @return void |
199
|
|
|
*/ |
200
|
|
|
public function info($message, array $context = []) { |
201
|
|
|
if ($this->containsThrowable($context)) { |
202
|
|
|
$this->logger->logException($context['exception'], array_merge( |
203
|
|
|
[ |
204
|
|
|
'message' => $message, |
205
|
|
|
'level' => ILogger::INFO, |
206
|
|
|
], |
207
|
|
|
$context |
208
|
|
|
)); |
209
|
|
|
} else { |
210
|
|
|
$this->logger->info($message, $context); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Detailed debug information. |
216
|
|
|
* |
217
|
|
|
* @param string $message |
218
|
|
|
* @param array $context |
219
|
|
|
* |
220
|
|
|
* @return void |
221
|
|
|
*/ |
222
|
|
|
public function debug($message, array $context = []) { |
223
|
|
|
if ($this->containsThrowable($context)) { |
224
|
|
|
$this->logger->logException($context['exception'], array_merge( |
225
|
|
|
[ |
226
|
|
|
'message' => $message, |
227
|
|
|
'level' => ILogger::DEBUG, |
228
|
|
|
], |
229
|
|
|
$context |
230
|
|
|
)); |
231
|
|
|
} else { |
232
|
|
|
$this->logger->debug($message, $context); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Logs with an arbitrary level. |
238
|
|
|
* |
239
|
|
|
* @param mixed $level |
240
|
|
|
* @param string $message |
241
|
|
|
* @param array $context |
242
|
|
|
* |
243
|
|
|
* @return void |
244
|
|
|
* |
245
|
|
|
* @throws InvalidArgumentException |
246
|
|
|
*/ |
247
|
|
|
public function log($level, $message, array $context = []) { |
248
|
|
|
if (!is_int($level) || $level < ILogger::DEBUG || $level > ILogger::FATAL) { |
249
|
|
|
throw new InvalidArgumentException('Nextcloud allows only integer log levels'); |
250
|
|
|
} |
251
|
|
|
if ($this->containsThrowable($context)) { |
252
|
|
|
$this->logger->logException($context['exception'], array_merge( |
253
|
|
|
[ |
254
|
|
|
'message' => $message, |
255
|
|
|
'level' => $level, |
256
|
|
|
], |
257
|
|
|
$context |
258
|
|
|
)); |
259
|
|
|
} else { |
260
|
|
|
$this->logger->log($level, $message, $context); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|