@@ -32,45 +32,45 @@ |
||
32 | 32 | */ |
33 | 33 | interface IRegistry { |
34 | 34 | |
35 | - /** |
|
36 | - * Register a reporter instance |
|
37 | - * |
|
38 | - * @param IReporter $reporter |
|
39 | - * |
|
40 | - * @since 13.0.0 |
|
41 | - */ |
|
42 | - public function register(IReporter $reporter): void; |
|
35 | + /** |
|
36 | + * Register a reporter instance |
|
37 | + * |
|
38 | + * @param IReporter $reporter |
|
39 | + * |
|
40 | + * @since 13.0.0 |
|
41 | + */ |
|
42 | + public function register(IReporter $reporter): void; |
|
43 | 43 | |
44 | - /** |
|
45 | - * Delegate breadcrumb collection to all registered reporters |
|
46 | - * |
|
47 | - * @param string $message |
|
48 | - * @param string $category |
|
49 | - * @param array $context |
|
50 | - * |
|
51 | - * @since 15.0.0 |
|
52 | - */ |
|
53 | - public function delegateBreadcrumb(string $message, string $category, array $context = []): void; |
|
44 | + /** |
|
45 | + * Delegate breadcrumb collection to all registered reporters |
|
46 | + * |
|
47 | + * @param string $message |
|
48 | + * @param string $category |
|
49 | + * @param array $context |
|
50 | + * |
|
51 | + * @since 15.0.0 |
|
52 | + */ |
|
53 | + public function delegateBreadcrumb(string $message, string $category, array $context = []): void; |
|
54 | 54 | |
55 | - /** |
|
56 | - * Delegate crash reporting to all registered reporters |
|
57 | - * |
|
58 | - * @param Exception|Throwable $exception |
|
59 | - * @param array $context |
|
60 | - * |
|
61 | - * @since 13.0.0 |
|
62 | - */ |
|
63 | - public function delegateReport($exception, array $context = []); |
|
55 | + /** |
|
56 | + * Delegate crash reporting to all registered reporters |
|
57 | + * |
|
58 | + * @param Exception|Throwable $exception |
|
59 | + * @param array $context |
|
60 | + * |
|
61 | + * @since 13.0.0 |
|
62 | + */ |
|
63 | + public function delegateReport($exception, array $context = []); |
|
64 | 64 | |
65 | - /** |
|
66 | - * Delegate a message to all reporters that implement IMessageReporter |
|
67 | - * |
|
68 | - * @param string $message |
|
69 | - * @param array $context |
|
70 | - * |
|
71 | - * @return void |
|
72 | - * |
|
73 | - * @since 17.0.0 |
|
74 | - */ |
|
75 | - public function delegateMessage(string $message, array $context = []): void; |
|
65 | + /** |
|
66 | + * Delegate a message to all reporters that implement IMessageReporter |
|
67 | + * |
|
68 | + * @param string $message |
|
69 | + * @param array $context |
|
70 | + * |
|
71 | + * @return void |
|
72 | + * |
|
73 | + * @since 17.0.0 |
|
74 | + */ |
|
75 | + public function delegateMessage(string $message, array $context = []): void; |
|
76 | 76 | } |
@@ -30,14 +30,14 @@ |
||
30 | 30 | */ |
31 | 31 | interface IMessageReporter extends IReporter { |
32 | 32 | |
33 | - /** |
|
34 | - * Report a (error) message |
|
35 | - * |
|
36 | - * @param string $message |
|
37 | - * @param array $context |
|
38 | - * |
|
39 | - * @since 17.0.0 |
|
40 | - */ |
|
41 | - public function reportMessage(string $message, array $context = []): void; |
|
33 | + /** |
|
34 | + * Report a (error) message |
|
35 | + * |
|
36 | + * @param string $message |
|
37 | + * @param array $context |
|
38 | + * |
|
39 | + * @since 17.0.0 |
|
40 | + */ |
|
41 | + public function reportMessage(string $message, array $context = []): void; |
|
42 | 42 | |
43 | 43 | } |
@@ -31,62 +31,62 @@ |
||
31 | 31 | |
32 | 32 | class Registry implements IRegistry { |
33 | 33 | |
34 | - /** @var IReporter[] */ |
|
35 | - private $reporters = []; |
|
34 | + /** @var IReporter[] */ |
|
35 | + private $reporters = []; |
|
36 | 36 | |
37 | - /** |
|
38 | - * Register a reporter instance |
|
39 | - * |
|
40 | - * @param IReporter $reporter |
|
41 | - */ |
|
42 | - public function register(IReporter $reporter): void { |
|
43 | - $this->reporters[] = $reporter; |
|
44 | - } |
|
37 | + /** |
|
38 | + * Register a reporter instance |
|
39 | + * |
|
40 | + * @param IReporter $reporter |
|
41 | + */ |
|
42 | + public function register(IReporter $reporter): void { |
|
43 | + $this->reporters[] = $reporter; |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Delegate breadcrumb collection to all registered reporters |
|
48 | - * |
|
49 | - * @param string $message |
|
50 | - * @param string $category |
|
51 | - * @param array $context |
|
52 | - * |
|
53 | - * @since 15.0.0 |
|
54 | - */ |
|
55 | - public function delegateBreadcrumb(string $message, string $category, array $context = []): void { |
|
56 | - foreach ($this->reporters as $reporter) { |
|
57 | - if ($reporter instanceof ICollectBreadcrumbs) { |
|
58 | - $reporter->collect($message, $category, $context); |
|
59 | - } |
|
60 | - } |
|
61 | - } |
|
46 | + /** |
|
47 | + * Delegate breadcrumb collection to all registered reporters |
|
48 | + * |
|
49 | + * @param string $message |
|
50 | + * @param string $category |
|
51 | + * @param array $context |
|
52 | + * |
|
53 | + * @since 15.0.0 |
|
54 | + */ |
|
55 | + public function delegateBreadcrumb(string $message, string $category, array $context = []): void { |
|
56 | + foreach ($this->reporters as $reporter) { |
|
57 | + if ($reporter instanceof ICollectBreadcrumbs) { |
|
58 | + $reporter->collect($message, $category, $context); |
|
59 | + } |
|
60 | + } |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Delegate crash reporting to all registered reporters |
|
65 | - * |
|
66 | - * @param Exception|Throwable $exception |
|
67 | - * @param array $context |
|
68 | - */ |
|
69 | - public function delegateReport($exception, array $context = []): void { |
|
70 | - /** @var IReporter $reporter */ |
|
71 | - foreach ($this->reporters as $reporter) { |
|
72 | - $reporter->report($exception, $context); |
|
73 | - } |
|
74 | - } |
|
63 | + /** |
|
64 | + * Delegate crash reporting to all registered reporters |
|
65 | + * |
|
66 | + * @param Exception|Throwable $exception |
|
67 | + * @param array $context |
|
68 | + */ |
|
69 | + public function delegateReport($exception, array $context = []): void { |
|
70 | + /** @var IReporter $reporter */ |
|
71 | + foreach ($this->reporters as $reporter) { |
|
72 | + $reporter->report($exception, $context); |
|
73 | + } |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Delegate a message to all reporters that implement IMessageReporter |
|
78 | - * |
|
79 | - * @param string $message |
|
80 | - * @param array $context |
|
81 | - * |
|
82 | - * @return void |
|
83 | - */ |
|
84 | - public function delegateMessage(string $message, array $context = []): void { |
|
85 | - foreach ($this->reporters as $reporter) { |
|
86 | - if ($reporter instanceof IMessageReporter) { |
|
87 | - $reporter->reportMessage($message, $context); |
|
88 | - } |
|
89 | - } |
|
90 | - } |
|
76 | + /** |
|
77 | + * Delegate a message to all reporters that implement IMessageReporter |
|
78 | + * |
|
79 | + * @param string $message |
|
80 | + * @param array $context |
|
81 | + * |
|
82 | + * @return void |
|
83 | + */ |
|
84 | + public function delegateMessage(string $message, array $context = []): void { |
|
85 | + foreach ($this->reporters as $reporter) { |
|
86 | + if ($reporter instanceof IMessageReporter) { |
|
87 | + $reporter->reportMessage($message, $context); |
|
88 | + } |
|
89 | + } |
|
90 | + } |
|
91 | 91 | |
92 | 92 | } |
@@ -55,295 +55,295 @@ |
||
55 | 55 | */ |
56 | 56 | class Log implements ILogger { |
57 | 57 | |
58 | - /** @var IWriter */ |
|
59 | - private $logger; |
|
60 | - |
|
61 | - /** @var SystemConfig */ |
|
62 | - private $config; |
|
63 | - |
|
64 | - /** @var boolean|null cache the result of the log condition check for the request */ |
|
65 | - private $logConditionSatisfied = null; |
|
66 | - |
|
67 | - /** @var Normalizer */ |
|
68 | - private $normalizer; |
|
69 | - |
|
70 | - /** @var IRegistry */ |
|
71 | - private $crashReporters; |
|
72 | - |
|
73 | - /** |
|
74 | - * @param IWriter $logger The logger that should be used |
|
75 | - * @param SystemConfig $config the system config object |
|
76 | - * @param Normalizer|null $normalizer |
|
77 | - * @param IRegistry|null $registry |
|
78 | - */ |
|
79 | - public function __construct(IWriter $logger, SystemConfig $config = null, $normalizer = null, IRegistry $registry = null) { |
|
80 | - // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
|
81 | - if ($config === null) { |
|
82 | - $config = \OC::$server->getSystemConfig(); |
|
83 | - } |
|
84 | - |
|
85 | - $this->config = $config; |
|
86 | - $this->logger = $logger; |
|
87 | - if ($normalizer === null) { |
|
88 | - $this->normalizer = new Normalizer(); |
|
89 | - } else { |
|
90 | - $this->normalizer = $normalizer; |
|
91 | - } |
|
92 | - $this->crashReporters = $registry; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * System is unusable. |
|
97 | - * |
|
98 | - * @param string $message |
|
99 | - * @param array $context |
|
100 | - * @return void |
|
101 | - */ |
|
102 | - public function emergency(string $message, array $context = []) { |
|
103 | - $this->log(ILogger::FATAL, $message, $context); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Action must be taken immediately. |
|
108 | - * |
|
109 | - * Example: Entire website down, database unavailable, etc. This should |
|
110 | - * trigger the SMS alerts and wake you up. |
|
111 | - * |
|
112 | - * @param string $message |
|
113 | - * @param array $context |
|
114 | - * @return void |
|
115 | - */ |
|
116 | - public function alert(string $message, array $context = []) { |
|
117 | - $this->log(ILogger::ERROR, $message, $context); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Critical conditions. |
|
122 | - * |
|
123 | - * Example: Application component unavailable, unexpected exception. |
|
124 | - * |
|
125 | - * @param string $message |
|
126 | - * @param array $context |
|
127 | - * @return void |
|
128 | - */ |
|
129 | - public function critical(string $message, array $context = []) { |
|
130 | - $this->log(ILogger::ERROR, $message, $context); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Runtime errors that do not require immediate action but should typically |
|
135 | - * be logged and monitored. |
|
136 | - * |
|
137 | - * @param string $message |
|
138 | - * @param array $context |
|
139 | - * @return void |
|
140 | - */ |
|
141 | - public function error(string $message, array $context = []) { |
|
142 | - $this->log(ILogger::ERROR, $message, $context); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Exceptional occurrences that are not errors. |
|
147 | - * |
|
148 | - * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
149 | - * that are not necessarily wrong. |
|
150 | - * |
|
151 | - * @param string $message |
|
152 | - * @param array $context |
|
153 | - * @return void |
|
154 | - */ |
|
155 | - public function warning(string $message, array $context = []) { |
|
156 | - $this->log(ILogger::WARN, $message, $context); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Normal but significant events. |
|
161 | - * |
|
162 | - * @param string $message |
|
163 | - * @param array $context |
|
164 | - * @return void |
|
165 | - */ |
|
166 | - public function notice(string $message, array $context = []) { |
|
167 | - $this->log(ILogger::INFO, $message, $context); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Interesting events. |
|
172 | - * |
|
173 | - * Example: User logs in, SQL logs. |
|
174 | - * |
|
175 | - * @param string $message |
|
176 | - * @param array $context |
|
177 | - * @return void |
|
178 | - */ |
|
179 | - public function info(string $message, array $context = []) { |
|
180 | - $this->log(ILogger::INFO, $message, $context); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * Detailed debug information. |
|
185 | - * |
|
186 | - * @param string $message |
|
187 | - * @param array $context |
|
188 | - * @return void |
|
189 | - */ |
|
190 | - public function debug(string $message, array $context = []) { |
|
191 | - $this->log(ILogger::DEBUG, $message, $context); |
|
192 | - } |
|
193 | - |
|
194 | - |
|
195 | - /** |
|
196 | - * Logs with an arbitrary level. |
|
197 | - * |
|
198 | - * @param int $level |
|
199 | - * @param string $message |
|
200 | - * @param array $context |
|
201 | - * @return void |
|
202 | - */ |
|
203 | - public function log(int $level, string $message, array $context = []) { |
|
204 | - $minLevel = $this->getLogLevel($context); |
|
205 | - |
|
206 | - array_walk($context, [$this->normalizer, 'format']); |
|
207 | - |
|
208 | - $app = $context['app'] ?? 'no app in context'; |
|
209 | - |
|
210 | - // interpolate $message as defined in PSR-3 |
|
211 | - $replace = []; |
|
212 | - foreach ($context as $key => $val) { |
|
213 | - $replace['{' . $key . '}'] = $val; |
|
214 | - } |
|
215 | - $message = strtr($message, $replace); |
|
216 | - |
|
217 | - if ($level >= $minLevel) { |
|
218 | - $this->writeLog($app, $message, $level); |
|
219 | - |
|
220 | - if ($this->crashReporters !== null) { |
|
221 | - $messageContext = array_merge( |
|
222 | - $context, |
|
223 | - [ |
|
224 | - 'level' => $level |
|
225 | - ] |
|
226 | - ); |
|
227 | - $this->crashReporters->delegateMessage($message, $messageContext); |
|
228 | - } |
|
229 | - } else { |
|
230 | - if ($this->crashReporters !== null) { |
|
231 | - $this->crashReporters->delegateBreadcrumb($message, 'log', $context); |
|
232 | - } |
|
233 | - } |
|
234 | - |
|
235 | - } |
|
236 | - |
|
237 | - private function getLogLevel($context) { |
|
238 | - $logCondition = $this->config->getValue('log.condition', []); |
|
239 | - |
|
240 | - /** |
|
241 | - * check for a special log condition - this enables an increased log on |
|
242 | - * a per request/user base |
|
243 | - */ |
|
244 | - if ($this->logConditionSatisfied === null) { |
|
245 | - // default to false to just process this once per request |
|
246 | - $this->logConditionSatisfied = false; |
|
247 | - if (!empty($logCondition)) { |
|
248 | - |
|
249 | - // check for secret token in the request |
|
250 | - if (isset($logCondition['shared_secret'])) { |
|
251 | - $request = \OC::$server->getRequest(); |
|
252 | - |
|
253 | - if ($request->getMethod() === 'PUT' && |
|
254 | - strpos($request->getHeader('Content-Type'), 'application/x-www-form-urlencoded') === false && |
|
255 | - strpos($request->getHeader('Content-Type'), 'application/json') === false) { |
|
256 | - $logSecretRequest = ''; |
|
257 | - } else { |
|
258 | - $logSecretRequest = $request->getParam('log_secret', ''); |
|
259 | - } |
|
260 | - |
|
261 | - // if token is found in the request change set the log condition to satisfied |
|
262 | - if ($request && hash_equals($logCondition['shared_secret'], $logSecretRequest)) { |
|
263 | - $this->logConditionSatisfied = true; |
|
264 | - } |
|
265 | - } |
|
266 | - |
|
267 | - // check for user |
|
268 | - if (isset($logCondition['users'])) { |
|
269 | - $user = \OC::$server->getUserSession()->getUser(); |
|
270 | - |
|
271 | - // if the user matches set the log condition to satisfied |
|
272 | - if ($user !== null && in_array($user->getUID(), $logCondition['users'], true)) { |
|
273 | - $this->logConditionSatisfied = true; |
|
274 | - } |
|
275 | - } |
|
276 | - } |
|
277 | - } |
|
278 | - |
|
279 | - // if log condition is satisfied change the required log level to DEBUG |
|
280 | - if ($this->logConditionSatisfied) { |
|
281 | - return ILogger::DEBUG; |
|
282 | - } |
|
283 | - |
|
284 | - if (isset($context['app'])) { |
|
285 | - $app = $context['app']; |
|
286 | - |
|
287 | - /** |
|
288 | - * check log condition based on the context of each log message |
|
289 | - * once this is met -> change the required log level to debug |
|
290 | - */ |
|
291 | - if (!empty($logCondition) |
|
292 | - && isset($logCondition['apps']) |
|
293 | - && in_array($app, $logCondition['apps'], true)) { |
|
294 | - return ILogger::DEBUG; |
|
295 | - } |
|
296 | - } |
|
297 | - |
|
298 | - return min($this->config->getValue('loglevel', ILogger::WARN), ILogger::FATAL); |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * Logs an exception very detailed |
|
303 | - * |
|
304 | - * @param \Exception|\Throwable $exception |
|
305 | - * @param array $context |
|
306 | - * @return void |
|
307 | - * @since 8.2.0 |
|
308 | - */ |
|
309 | - public function logException(\Throwable $exception, array $context = []) { |
|
310 | - $app = $context['app'] ?? 'no app in context'; |
|
311 | - $level = $context['level'] ?? ILogger::ERROR; |
|
312 | - |
|
313 | - $serializer = new ExceptionSerializer(); |
|
314 | - $data = $serializer->serializeException($exception); |
|
315 | - $data['CustomMessage'] = $context['message'] ?? '--'; |
|
316 | - |
|
317 | - $minLevel = $this->getLogLevel($context); |
|
318 | - |
|
319 | - array_walk($context, [$this->normalizer, 'format']); |
|
320 | - |
|
321 | - if ($level >= $minLevel) { |
|
322 | - if (!$this->logger instanceof IFileBased) { |
|
323 | - $data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR); |
|
324 | - } |
|
325 | - $this->writeLog($app, $data, $level); |
|
326 | - } |
|
327 | - |
|
328 | - $context['level'] = $level; |
|
329 | - if (!is_null($this->crashReporters)) { |
|
330 | - $this->crashReporters->delegateReport($exception, $context); |
|
331 | - } |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * @param string $app |
|
336 | - * @param string|array $entry |
|
337 | - * @param int $level |
|
338 | - */ |
|
339 | - protected function writeLog(string $app, $entry, int $level) { |
|
340 | - $this->logger->write($app, $entry, $level); |
|
341 | - } |
|
342 | - |
|
343 | - public function getLogPath():string { |
|
344 | - if($this->logger instanceof IFileBased) { |
|
345 | - return $this->logger->getLogFilePath(); |
|
346 | - } |
|
347 | - throw new \RuntimeException('Log implementation has no path'); |
|
348 | - } |
|
58 | + /** @var IWriter */ |
|
59 | + private $logger; |
|
60 | + |
|
61 | + /** @var SystemConfig */ |
|
62 | + private $config; |
|
63 | + |
|
64 | + /** @var boolean|null cache the result of the log condition check for the request */ |
|
65 | + private $logConditionSatisfied = null; |
|
66 | + |
|
67 | + /** @var Normalizer */ |
|
68 | + private $normalizer; |
|
69 | + |
|
70 | + /** @var IRegistry */ |
|
71 | + private $crashReporters; |
|
72 | + |
|
73 | + /** |
|
74 | + * @param IWriter $logger The logger that should be used |
|
75 | + * @param SystemConfig $config the system config object |
|
76 | + * @param Normalizer|null $normalizer |
|
77 | + * @param IRegistry|null $registry |
|
78 | + */ |
|
79 | + public function __construct(IWriter $logger, SystemConfig $config = null, $normalizer = null, IRegistry $registry = null) { |
|
80 | + // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
|
81 | + if ($config === null) { |
|
82 | + $config = \OC::$server->getSystemConfig(); |
|
83 | + } |
|
84 | + |
|
85 | + $this->config = $config; |
|
86 | + $this->logger = $logger; |
|
87 | + if ($normalizer === null) { |
|
88 | + $this->normalizer = new Normalizer(); |
|
89 | + } else { |
|
90 | + $this->normalizer = $normalizer; |
|
91 | + } |
|
92 | + $this->crashReporters = $registry; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * System is unusable. |
|
97 | + * |
|
98 | + * @param string $message |
|
99 | + * @param array $context |
|
100 | + * @return void |
|
101 | + */ |
|
102 | + public function emergency(string $message, array $context = []) { |
|
103 | + $this->log(ILogger::FATAL, $message, $context); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Action must be taken immediately. |
|
108 | + * |
|
109 | + * Example: Entire website down, database unavailable, etc. This should |
|
110 | + * trigger the SMS alerts and wake you up. |
|
111 | + * |
|
112 | + * @param string $message |
|
113 | + * @param array $context |
|
114 | + * @return void |
|
115 | + */ |
|
116 | + public function alert(string $message, array $context = []) { |
|
117 | + $this->log(ILogger::ERROR, $message, $context); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Critical conditions. |
|
122 | + * |
|
123 | + * Example: Application component unavailable, unexpected exception. |
|
124 | + * |
|
125 | + * @param string $message |
|
126 | + * @param array $context |
|
127 | + * @return void |
|
128 | + */ |
|
129 | + public function critical(string $message, array $context = []) { |
|
130 | + $this->log(ILogger::ERROR, $message, $context); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Runtime errors that do not require immediate action but should typically |
|
135 | + * be logged and monitored. |
|
136 | + * |
|
137 | + * @param string $message |
|
138 | + * @param array $context |
|
139 | + * @return void |
|
140 | + */ |
|
141 | + public function error(string $message, array $context = []) { |
|
142 | + $this->log(ILogger::ERROR, $message, $context); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Exceptional occurrences that are not errors. |
|
147 | + * |
|
148 | + * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
149 | + * that are not necessarily wrong. |
|
150 | + * |
|
151 | + * @param string $message |
|
152 | + * @param array $context |
|
153 | + * @return void |
|
154 | + */ |
|
155 | + public function warning(string $message, array $context = []) { |
|
156 | + $this->log(ILogger::WARN, $message, $context); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Normal but significant events. |
|
161 | + * |
|
162 | + * @param string $message |
|
163 | + * @param array $context |
|
164 | + * @return void |
|
165 | + */ |
|
166 | + public function notice(string $message, array $context = []) { |
|
167 | + $this->log(ILogger::INFO, $message, $context); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Interesting events. |
|
172 | + * |
|
173 | + * Example: User logs in, SQL logs. |
|
174 | + * |
|
175 | + * @param string $message |
|
176 | + * @param array $context |
|
177 | + * @return void |
|
178 | + */ |
|
179 | + public function info(string $message, array $context = []) { |
|
180 | + $this->log(ILogger::INFO, $message, $context); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * Detailed debug information. |
|
185 | + * |
|
186 | + * @param string $message |
|
187 | + * @param array $context |
|
188 | + * @return void |
|
189 | + */ |
|
190 | + public function debug(string $message, array $context = []) { |
|
191 | + $this->log(ILogger::DEBUG, $message, $context); |
|
192 | + } |
|
193 | + |
|
194 | + |
|
195 | + /** |
|
196 | + * Logs with an arbitrary level. |
|
197 | + * |
|
198 | + * @param int $level |
|
199 | + * @param string $message |
|
200 | + * @param array $context |
|
201 | + * @return void |
|
202 | + */ |
|
203 | + public function log(int $level, string $message, array $context = []) { |
|
204 | + $minLevel = $this->getLogLevel($context); |
|
205 | + |
|
206 | + array_walk($context, [$this->normalizer, 'format']); |
|
207 | + |
|
208 | + $app = $context['app'] ?? 'no app in context'; |
|
209 | + |
|
210 | + // interpolate $message as defined in PSR-3 |
|
211 | + $replace = []; |
|
212 | + foreach ($context as $key => $val) { |
|
213 | + $replace['{' . $key . '}'] = $val; |
|
214 | + } |
|
215 | + $message = strtr($message, $replace); |
|
216 | + |
|
217 | + if ($level >= $minLevel) { |
|
218 | + $this->writeLog($app, $message, $level); |
|
219 | + |
|
220 | + if ($this->crashReporters !== null) { |
|
221 | + $messageContext = array_merge( |
|
222 | + $context, |
|
223 | + [ |
|
224 | + 'level' => $level |
|
225 | + ] |
|
226 | + ); |
|
227 | + $this->crashReporters->delegateMessage($message, $messageContext); |
|
228 | + } |
|
229 | + } else { |
|
230 | + if ($this->crashReporters !== null) { |
|
231 | + $this->crashReporters->delegateBreadcrumb($message, 'log', $context); |
|
232 | + } |
|
233 | + } |
|
234 | + |
|
235 | + } |
|
236 | + |
|
237 | + private function getLogLevel($context) { |
|
238 | + $logCondition = $this->config->getValue('log.condition', []); |
|
239 | + |
|
240 | + /** |
|
241 | + * check for a special log condition - this enables an increased log on |
|
242 | + * a per request/user base |
|
243 | + */ |
|
244 | + if ($this->logConditionSatisfied === null) { |
|
245 | + // default to false to just process this once per request |
|
246 | + $this->logConditionSatisfied = false; |
|
247 | + if (!empty($logCondition)) { |
|
248 | + |
|
249 | + // check for secret token in the request |
|
250 | + if (isset($logCondition['shared_secret'])) { |
|
251 | + $request = \OC::$server->getRequest(); |
|
252 | + |
|
253 | + if ($request->getMethod() === 'PUT' && |
|
254 | + strpos($request->getHeader('Content-Type'), 'application/x-www-form-urlencoded') === false && |
|
255 | + strpos($request->getHeader('Content-Type'), 'application/json') === false) { |
|
256 | + $logSecretRequest = ''; |
|
257 | + } else { |
|
258 | + $logSecretRequest = $request->getParam('log_secret', ''); |
|
259 | + } |
|
260 | + |
|
261 | + // if token is found in the request change set the log condition to satisfied |
|
262 | + if ($request && hash_equals($logCondition['shared_secret'], $logSecretRequest)) { |
|
263 | + $this->logConditionSatisfied = true; |
|
264 | + } |
|
265 | + } |
|
266 | + |
|
267 | + // check for user |
|
268 | + if (isset($logCondition['users'])) { |
|
269 | + $user = \OC::$server->getUserSession()->getUser(); |
|
270 | + |
|
271 | + // if the user matches set the log condition to satisfied |
|
272 | + if ($user !== null && in_array($user->getUID(), $logCondition['users'], true)) { |
|
273 | + $this->logConditionSatisfied = true; |
|
274 | + } |
|
275 | + } |
|
276 | + } |
|
277 | + } |
|
278 | + |
|
279 | + // if log condition is satisfied change the required log level to DEBUG |
|
280 | + if ($this->logConditionSatisfied) { |
|
281 | + return ILogger::DEBUG; |
|
282 | + } |
|
283 | + |
|
284 | + if (isset($context['app'])) { |
|
285 | + $app = $context['app']; |
|
286 | + |
|
287 | + /** |
|
288 | + * check log condition based on the context of each log message |
|
289 | + * once this is met -> change the required log level to debug |
|
290 | + */ |
|
291 | + if (!empty($logCondition) |
|
292 | + && isset($logCondition['apps']) |
|
293 | + && in_array($app, $logCondition['apps'], true)) { |
|
294 | + return ILogger::DEBUG; |
|
295 | + } |
|
296 | + } |
|
297 | + |
|
298 | + return min($this->config->getValue('loglevel', ILogger::WARN), ILogger::FATAL); |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * Logs an exception very detailed |
|
303 | + * |
|
304 | + * @param \Exception|\Throwable $exception |
|
305 | + * @param array $context |
|
306 | + * @return void |
|
307 | + * @since 8.2.0 |
|
308 | + */ |
|
309 | + public function logException(\Throwable $exception, array $context = []) { |
|
310 | + $app = $context['app'] ?? 'no app in context'; |
|
311 | + $level = $context['level'] ?? ILogger::ERROR; |
|
312 | + |
|
313 | + $serializer = new ExceptionSerializer(); |
|
314 | + $data = $serializer->serializeException($exception); |
|
315 | + $data['CustomMessage'] = $context['message'] ?? '--'; |
|
316 | + |
|
317 | + $minLevel = $this->getLogLevel($context); |
|
318 | + |
|
319 | + array_walk($context, [$this->normalizer, 'format']); |
|
320 | + |
|
321 | + if ($level >= $minLevel) { |
|
322 | + if (!$this->logger instanceof IFileBased) { |
|
323 | + $data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR); |
|
324 | + } |
|
325 | + $this->writeLog($app, $data, $level); |
|
326 | + } |
|
327 | + |
|
328 | + $context['level'] = $level; |
|
329 | + if (!is_null($this->crashReporters)) { |
|
330 | + $this->crashReporters->delegateReport($exception, $context); |
|
331 | + } |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * @param string $app |
|
336 | + * @param string|array $entry |
|
337 | + * @param int $level |
|
338 | + */ |
|
339 | + protected function writeLog(string $app, $entry, int $level) { |
|
340 | + $this->logger->write($app, $entry, $level); |
|
341 | + } |
|
342 | + |
|
343 | + public function getLogPath():string { |
|
344 | + if($this->logger instanceof IFileBased) { |
|
345 | + return $this->logger->getLogFilePath(); |
|
346 | + } |
|
347 | + throw new \RuntimeException('Log implementation has no path'); |
|
348 | + } |
|
349 | 349 | } |