Completed
Push — master ( f778da...5767f1 )
by Roeland
12:07
created
lib/private/Log.php 1 patch
Indentation   +280 added lines, -280 removed lines patch added patch discarded remove patch
@@ -55,284 +55,284 @@
 block discarded – undo
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
-
221
-		if (!is_null($this->crashReporters)) {
222
-			$this->crashReporters->delegateBreadcrumb($message, 'log', $context);
223
-		}
224
-	}
225
-
226
-	private function getLogLevel($context) {
227
-		$logCondition = $this->config->getValue('log.condition', []);
228
-
229
-		/**
230
-		 * check for a special log condition - this enables an increased log on
231
-		 * a per request/user base
232
-		 */
233
-		if ($this->logConditionSatisfied === null) {
234
-			// default to false to just process this once per request
235
-			$this->logConditionSatisfied = false;
236
-			if (!empty($logCondition)) {
237
-
238
-				// check for secret token in the request
239
-				if (isset($logCondition['shared_secret'])) {
240
-					$request = \OC::$server->getRequest();
241
-
242
-					if ($request->getMethod() === 'PUT' &&
243
-						strpos($request->getHeader('Content-Type'), 'application/x-www-form-urlencoded') === false &&
244
-						strpos($request->getHeader('Content-Type'), 'application/json') === false) {
245
-						$logSecretRequest = '';
246
-					} else {
247
-						$logSecretRequest = $request->getParam('log_secret', '');
248
-					}
249
-
250
-					// if token is found in the request change set the log condition to satisfied
251
-					if ($request && hash_equals($logCondition['shared_secret'], $logSecretRequest)) {
252
-						$this->logConditionSatisfied = true;
253
-					}
254
-				}
255
-
256
-				// check for user
257
-				if (isset($logCondition['users'])) {
258
-					$user = \OC::$server->getUserSession()->getUser();
259
-
260
-					// if the user matches set the log condition to satisfied
261
-					if ($user !== null && in_array($user->getUID(), $logCondition['users'], true)) {
262
-						$this->logConditionSatisfied = true;
263
-					}
264
-				}
265
-			}
266
-		}
267
-
268
-		// if log condition is satisfied change the required log level to DEBUG
269
-		if ($this->logConditionSatisfied) {
270
-			return ILogger::DEBUG;
271
-		}
272
-
273
-		if (isset($context['app'])) {
274
-			$app = $context['app'];
275
-
276
-			/**
277
-			 * check log condition based on the context of each log message
278
-			 * once this is met -> change the required log level to debug
279
-			 */
280
-			if (!empty($logCondition)
281
-				&& isset($logCondition['apps'])
282
-				&& in_array($app, $logCondition['apps'], true)) {
283
-				return ILogger::DEBUG;
284
-			}
285
-		}
286
-
287
-		return min($this->config->getValue('loglevel', ILogger::WARN), ILogger::FATAL);
288
-	}
289
-
290
-	/**
291
-	 * Logs an exception very detailed
292
-	 *
293
-	 * @param \Exception|\Throwable $exception
294
-	 * @param array $context
295
-	 * @return void
296
-	 * @since 8.2.0
297
-	 */
298
-	public function logException(\Throwable $exception, array $context = []) {
299
-		$app = $context['app'] ?? 'no app in context';
300
-		$level = $context['level'] ?? ILogger::ERROR;
301
-
302
-		$serializer = new ExceptionSerializer();
303
-		$data = $serializer->serializeException($exception);
304
-		$data['CustomMessage'] = $context['message'] ?? '--';
305
-
306
-		$minLevel = $this->getLogLevel($context);
307
-
308
-		array_walk($context, [$this->normalizer, 'format']);
309
-
310
-		if ($level >= $minLevel) {
311
-			if (!$this->logger instanceof IFileBased) {
312
-				$data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR);
313
-			}
314
-			$this->writeLog($app, $data, $level);
315
-		}
316
-
317
-		$context['level'] = $level;
318
-		if (!is_null($this->crashReporters)) {
319
-			$this->crashReporters->delegateReport($exception, $context);
320
-		}
321
-	}
322
-
323
-	/**
324
-	 * @param string $app
325
-	 * @param string|array $entry
326
-	 * @param int $level
327
-	 */
328
-	protected function writeLog(string $app, $entry, int $level) {
329
-		$this->logger->write($app, $entry, $level);
330
-	}
331
-
332
-	public function getLogPath():string {
333
-		if($this->logger instanceof IFileBased) {
334
-			return $this->logger->getLogFilePath();
335
-		}
336
-		throw new \RuntimeException('Log implementation has no path');
337
-	}
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
+
221
+        if (!is_null($this->crashReporters)) {
222
+            $this->crashReporters->delegateBreadcrumb($message, 'log', $context);
223
+        }
224
+    }
225
+
226
+    private function getLogLevel($context) {
227
+        $logCondition = $this->config->getValue('log.condition', []);
228
+
229
+        /**
230
+         * check for a special log condition - this enables an increased log on
231
+         * a per request/user base
232
+         */
233
+        if ($this->logConditionSatisfied === null) {
234
+            // default to false to just process this once per request
235
+            $this->logConditionSatisfied = false;
236
+            if (!empty($logCondition)) {
237
+
238
+                // check for secret token in the request
239
+                if (isset($logCondition['shared_secret'])) {
240
+                    $request = \OC::$server->getRequest();
241
+
242
+                    if ($request->getMethod() === 'PUT' &&
243
+                        strpos($request->getHeader('Content-Type'), 'application/x-www-form-urlencoded') === false &&
244
+                        strpos($request->getHeader('Content-Type'), 'application/json') === false) {
245
+                        $logSecretRequest = '';
246
+                    } else {
247
+                        $logSecretRequest = $request->getParam('log_secret', '');
248
+                    }
249
+
250
+                    // if token is found in the request change set the log condition to satisfied
251
+                    if ($request && hash_equals($logCondition['shared_secret'], $logSecretRequest)) {
252
+                        $this->logConditionSatisfied = true;
253
+                    }
254
+                }
255
+
256
+                // check for user
257
+                if (isset($logCondition['users'])) {
258
+                    $user = \OC::$server->getUserSession()->getUser();
259
+
260
+                    // if the user matches set the log condition to satisfied
261
+                    if ($user !== null && in_array($user->getUID(), $logCondition['users'], true)) {
262
+                        $this->logConditionSatisfied = true;
263
+                    }
264
+                }
265
+            }
266
+        }
267
+
268
+        // if log condition is satisfied change the required log level to DEBUG
269
+        if ($this->logConditionSatisfied) {
270
+            return ILogger::DEBUG;
271
+        }
272
+
273
+        if (isset($context['app'])) {
274
+            $app = $context['app'];
275
+
276
+            /**
277
+             * check log condition based on the context of each log message
278
+             * once this is met -> change the required log level to debug
279
+             */
280
+            if (!empty($logCondition)
281
+                && isset($logCondition['apps'])
282
+                && in_array($app, $logCondition['apps'], true)) {
283
+                return ILogger::DEBUG;
284
+            }
285
+        }
286
+
287
+        return min($this->config->getValue('loglevel', ILogger::WARN), ILogger::FATAL);
288
+    }
289
+
290
+    /**
291
+     * Logs an exception very detailed
292
+     *
293
+     * @param \Exception|\Throwable $exception
294
+     * @param array $context
295
+     * @return void
296
+     * @since 8.2.0
297
+     */
298
+    public function logException(\Throwable $exception, array $context = []) {
299
+        $app = $context['app'] ?? 'no app in context';
300
+        $level = $context['level'] ?? ILogger::ERROR;
301
+
302
+        $serializer = new ExceptionSerializer();
303
+        $data = $serializer->serializeException($exception);
304
+        $data['CustomMessage'] = $context['message'] ?? '--';
305
+
306
+        $minLevel = $this->getLogLevel($context);
307
+
308
+        array_walk($context, [$this->normalizer, 'format']);
309
+
310
+        if ($level >= $minLevel) {
311
+            if (!$this->logger instanceof IFileBased) {
312
+                $data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR);
313
+            }
314
+            $this->writeLog($app, $data, $level);
315
+        }
316
+
317
+        $context['level'] = $level;
318
+        if (!is_null($this->crashReporters)) {
319
+            $this->crashReporters->delegateReport($exception, $context);
320
+        }
321
+    }
322
+
323
+    /**
324
+     * @param string $app
325
+     * @param string|array $entry
326
+     * @param int $level
327
+     */
328
+    protected function writeLog(string $app, $entry, int $level) {
329
+        $this->logger->write($app, $entry, $level);
330
+    }
331
+
332
+    public function getLogPath():string {
333
+        if($this->logger instanceof IFileBased) {
334
+            return $this->logger->getLogFilePath();
335
+        }
336
+        throw new \RuntimeException('Log implementation has no path');
337
+    }
338 338
 }
Please login to merge, or discard this patch.
lib/private/Support/CrashReport/Registry.php 1 patch
Indentation   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -30,46 +30,46 @@
 block discarded – undo
30 30
 
31 31
 class Registry implements IRegistry {
32 32
 
33
-	/** @var IReporter[] */
34
-	private $reporters = [];
33
+    /** @var IReporter[] */
34
+    private $reporters = [];
35 35
 
36
-	/**
37
-	 * Register a reporter instance
38
-	 *
39
-	 * @param IReporter $reporter
40
-	 */
41
-	public function register(IReporter $reporter) {
42
-		$this->reporters[] = $reporter;
43
-	}
36
+    /**
37
+     * Register a reporter instance
38
+     *
39
+     * @param IReporter $reporter
40
+     */
41
+    public function register(IReporter $reporter) {
42
+        $this->reporters[] = $reporter;
43
+    }
44 44
 
45
-	/**
46
-	 * Delegate breadcrumb collection to all registered reporters
47
-	 *
48
-	 * @param string $message
49
-	 * @param string $category
50
-	 * @param array $context
51
-	 *
52
-	 * @since 15.0.0
53
-	 */
54
-	public function delegateBreadcrumb(string $message, string $category, array $context = []) {
55
-		foreach ($this->reporters as $reporter) {
56
-			if ($reporter instanceof ICollectBreadcrumbs) {
57
-				$reporter->collect($message, $category, $context);
58
-			}
59
-		}
60
-	}
45
+    /**
46
+     * Delegate breadcrumb collection to all registered reporters
47
+     *
48
+     * @param string $message
49
+     * @param string $category
50
+     * @param array $context
51
+     *
52
+     * @since 15.0.0
53
+     */
54
+    public function delegateBreadcrumb(string $message, string $category, array $context = []) {
55
+        foreach ($this->reporters as $reporter) {
56
+            if ($reporter instanceof ICollectBreadcrumbs) {
57
+                $reporter->collect($message, $category, $context);
58
+            }
59
+        }
60
+    }
61 61
 
62
-	/**
63
-	 * Delegate crash reporting to all registered reporters
64
-	 *
65
-	 * @param Exception|Throwable $exception
66
-	 * @param array $context
67
-	 */
68
-	public function delegateReport($exception, array $context = []) {
69
-		/** @var IReporter $reporter */
70
-		foreach ($this->reporters as $reporter) {
71
-			$reporter->report($exception, $context);
72
-		}
73
-	}
62
+    /**
63
+     * Delegate crash reporting to all registered reporters
64
+     *
65
+     * @param Exception|Throwable $exception
66
+     * @param array $context
67
+     */
68
+    public function delegateReport($exception, array $context = []) {
69
+        /** @var IReporter $reporter */
70
+        foreach ($this->reporters as $reporter) {
71
+            $reporter->report($exception, $context);
72
+        }
73
+    }
74 74
 
75 75
 }
Please login to merge, or discard this patch.
lib/public/Support/CrashReport/ICollectBreadcrumbs.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -29,15 +29,15 @@
 block discarded – undo
29 29
  */
30 30
 interface ICollectBreadcrumbs extends IReporter {
31 31
 
32
-	/**
33
-	 * Collect breadcrumbs for crash reports
34
-	 *
35
-	 * @param string $message
36
-	 * @param string $category
37
-	 * @param array $context
38
-	 *
39
-	 * @since 15.0.0
40
-	 */
41
-	public function collect(string $message, string $category, array $context = []);
32
+    /**
33
+     * Collect breadcrumbs for crash reports
34
+     *
35
+     * @param string $message
36
+     * @param string $category
37
+     * @param array $context
38
+     *
39
+     * @since 15.0.0
40
+     */
41
+    public function collect(string $message, string $category, array $context = []);
42 42
 
43 43
 }
Please login to merge, or discard this patch.
lib/public/Support/CrashReport/IRegistry.php 1 patch
Indentation   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -30,31 +30,31 @@
 block discarded – undo
30 30
  */
31 31
 interface IRegistry {
32 32
 
33
-	/**
34
-	 * Register a reporter instance
35
-	 *
36
-	 * @since 13.0.0
37
-	 * @param IReporter $reporter
38
-	 */
39
-	public function register(IReporter $reporter);
33
+    /**
34
+     * Register a reporter instance
35
+     *
36
+     * @since 13.0.0
37
+     * @param IReporter $reporter
38
+     */
39
+    public function register(IReporter $reporter);
40 40
 
41
-	/**
42
-	 * Delegate breadcrumb collection to all registered reporters
43
-	 *
44
-	 * @param string $message
45
-	 * @param string $category
46
-	 * @param array $context
47
-	 *
48
-	 * @since 15.0.0
49
-	 */
50
-	public function delegateBreadcrumb(string $message, string $category, array $context = []);
41
+    /**
42
+     * Delegate breadcrumb collection to all registered reporters
43
+     *
44
+     * @param string $message
45
+     * @param string $category
46
+     * @param array $context
47
+     *
48
+     * @since 15.0.0
49
+     */
50
+    public function delegateBreadcrumb(string $message, string $category, array $context = []);
51 51
 
52
-	/**
53
-	 * Delegate crash reporting to all registered reporters
54
-	 *
55
-	 * @since 13.0.0
56
-	 * @param Exception|Throwable $exception
57
-	 * @param array $context
58
-	 */
59
-	public function delegateReport($exception, array $context = []);
52
+    /**
53
+     * Delegate crash reporting to all registered reporters
54
+     *
55
+     * @since 13.0.0
56
+     * @param Exception|Throwable $exception
57
+     * @param array $context
58
+     */
59
+    public function delegateReport($exception, array $context = []);
60 60
 }
Please login to merge, or discard this patch.