Completed
Pull Request — master (#6329)
by Joas
19:15
created
lib/private/Log.php 2 patches
Indentation   +309 added lines, -309 removed lines patch added patch discarded remove patch
@@ -48,313 +48,313 @@
 block discarded – undo
48 48
 
49 49
 class Log implements ILogger {
50 50
 
51
-	/** @var string */
52
-	private $logger;
53
-
54
-	/** @var SystemConfig */
55
-	private $config;
56
-
57
-	/** @var boolean|null cache the result of the log condition check for the request */
58
-	private $logConditionSatisfied = null;
59
-
60
-	/** @var Normalizer */
61
-	private $normalizer;
62
-
63
-	protected $methodsWithSensitiveParameters = [
64
-		// Session/User
65
-		'completeLogin',
66
-		'login',
67
-		'checkPassword',
68
-		'loginWithPassword',
69
-		'updatePrivateKeyPassword',
70
-		'validateUserPass',
71
-
72
-		// TokenProvider
73
-		'getToken',
74
-		'isTokenPassword',
75
-		'getPassword',
76
-		'decryptPassword',
77
-		'logClientIn',
78
-		'generateToken',
79
-		'validateToken',
80
-
81
-		// TwoFactorAuth
82
-		'solveChallenge',
83
-		'verifyChallenge',
84
-
85
-		// ICrypto
86
-		'calculateHMAC',
87
-		'encrypt',
88
-		'decrypt',
89
-
90
-		// LoginController
91
-		'tryLogin',
92
-		'confirmPassword',
93
-
94
-		// LDAP
95
-		'bind',
96
-		'areCredentialsValid',
97
-		'invokeLDAPMethod',
98
-		'checkPasswordNoLogging',
99
-	];
100
-
101
-	/**
102
-	 * @param string $logger The logger that should be used
103
-	 * @param SystemConfig $config the system config object
104
-	 * @param null $normalizer
105
-	 */
106
-	public function __construct($logger = null, SystemConfig $config = null, $normalizer = null) {
107
-		// FIXME: Add this for backwards compatibility, should be fixed at some point probably
108
-		if($config === null) {
109
-			$config = \OC::$server->getSystemConfig();
110
-		}
111
-
112
-		$this->config = $config;
113
-
114
-		// FIXME: Add this for backwards compatibility, should be fixed at some point probably
115
-		if($logger === null) {
116
-			$logType = $this->config->getValue('log_type', 'file');
117
-			$this->logger = static::getLogClass($logType);
118
-			call_user_func(array($this->logger, 'init'));
119
-		} else {
120
-			$this->logger = $logger;
121
-		}
122
-		if ($normalizer === null) {
123
-			$this->normalizer = new Normalizer();
124
-		} else {
125
-			$this->normalizer = $normalizer;
126
-		}
127
-
128
-	}
129
-
130
-	/**
131
-	 * System is unusable.
132
-	 *
133
-	 * @param string $message
134
-	 * @param array $context
135
-	 * @return void
136
-	 */
137
-	public function emergency($message, array $context = array()) {
138
-		$this->log(Util::FATAL, $message, $context);
139
-	}
140
-
141
-	/**
142
-	 * Action must be taken immediately.
143
-	 *
144
-	 * Example: Entire website down, database unavailable, etc. This should
145
-	 * trigger the SMS alerts and wake you up.
146
-	 *
147
-	 * @param string $message
148
-	 * @param array $context
149
-	 * @return void
150
-	 */
151
-	public function alert($message, array $context = array()) {
152
-		$this->log(Util::ERROR, $message, $context);
153
-	}
154
-
155
-	/**
156
-	 * Critical conditions.
157
-	 *
158
-	 * Example: Application component unavailable, unexpected exception.
159
-	 *
160
-	 * @param string $message
161
-	 * @param array $context
162
-	 * @return void
163
-	 */
164
-	public function critical($message, array $context = array()) {
165
-		$this->log(Util::ERROR, $message, $context);
166
-	}
167
-
168
-	/**
169
-	 * Runtime errors that do not require immediate action but should typically
170
-	 * be logged and monitored.
171
-	 *
172
-	 * @param string $message
173
-	 * @param array $context
174
-	 * @return void
175
-	 */
176
-	public function error($message, array $context = array()) {
177
-		$this->log(Util::ERROR, $message, $context);
178
-	}
179
-
180
-	/**
181
-	 * Exceptional occurrences that are not errors.
182
-	 *
183
-	 * Example: Use of deprecated APIs, poor use of an API, undesirable things
184
-	 * that are not necessarily wrong.
185
-	 *
186
-	 * @param string $message
187
-	 * @param array $context
188
-	 * @return void
189
-	 */
190
-	public function warning($message, array $context = array()) {
191
-		$this->log(Util::WARN, $message, $context);
192
-	}
193
-
194
-	/**
195
-	 * Normal but significant events.
196
-	 *
197
-	 * @param string $message
198
-	 * @param array $context
199
-	 * @return void
200
-	 */
201
-	public function notice($message, array $context = array()) {
202
-		$this->log(Util::INFO, $message, $context);
203
-	}
204
-
205
-	/**
206
-	 * Interesting events.
207
-	 *
208
-	 * Example: User logs in, SQL logs.
209
-	 *
210
-	 * @param string $message
211
-	 * @param array $context
212
-	 * @return void
213
-	 */
214
-	public function info($message, array $context = array()) {
215
-		$this->log(Util::INFO, $message, $context);
216
-	}
217
-
218
-	/**
219
-	 * Detailed debug information.
220
-	 *
221
-	 * @param string $message
222
-	 * @param array $context
223
-	 * @return void
224
-	 */
225
-	public function debug($message, array $context = array()) {
226
-		$this->log(Util::DEBUG, $message, $context);
227
-	}
228
-
229
-
230
-	/**
231
-	 * Logs with an arbitrary level.
232
-	 *
233
-	 * @param mixed $level
234
-	 * @param string $message
235
-	 * @param array $context
236
-	 * @return void
237
-	 */
238
-	public function log($level, $message, array $context = array()) {
239
-		$minLevel = min($this->config->getValue('loglevel', Util::WARN), Util::FATAL);
240
-		$logCondition = $this->config->getValue('log.condition', []);
241
-
242
-		array_walk($context, [$this->normalizer, 'format']);
243
-
244
-		if (isset($context['app'])) {
245
-			$app = $context['app'];
246
-
247
-			/**
248
-			 * check log condition based on the context of each log message
249
-			 * once this is met -> change the required log level to debug
250
-			 */
251
-			if(!empty($logCondition)
252
-				&& isset($logCondition['apps'])
253
-				&& in_array($app, $logCondition['apps'], true)) {
254
-				$minLevel = Util::DEBUG;
255
-			}
256
-
257
-		} else {
258
-			$app = 'no app in context';
259
-		}
260
-		// interpolate $message as defined in PSR-3
261
-		$replace = array();
262
-		foreach ($context as $key => $val) {
263
-			$replace['{' . $key . '}'] = $val;
264
-		}
265
-
266
-		// interpolate replacement values into the message and return
267
-		$message = strtr($message, $replace);
268
-
269
-		/**
270
-		 * check for a special log condition - this enables an increased log on
271
-		 * a per request/user base
272
-		 */
273
-		if($this->logConditionSatisfied === null) {
274
-			// default to false to just process this once per request
275
-			$this->logConditionSatisfied = false;
276
-			if(!empty($logCondition)) {
277
-
278
-				// check for secret token in the request
279
-				if(isset($logCondition['shared_secret'])) {
280
-					$request = \OC::$server->getRequest();
281
-
282
-					// if token is found in the request change set the log condition to satisfied
283
-					if($request && hash_equals($logCondition['shared_secret'], $request->getParam('log_secret', ''))) {
284
-						$this->logConditionSatisfied = true;
285
-					}
286
-				}
287
-
288
-				// check for user
289
-				if(isset($logCondition['users'])) {
290
-					$user = \OC::$server->getUserSession()->getUser();
291
-
292
-					// if the user matches set the log condition to satisfied
293
-					if($user !== null && in_array($user->getUID(), $logCondition['users'], true)) {
294
-						$this->logConditionSatisfied = true;
295
-					}
296
-				}
297
-			}
298
-		}
299
-
300
-		// if log condition is satisfied change the required log level to DEBUG
301
-		if($this->logConditionSatisfied) {
302
-			$minLevel = Util::DEBUG;
303
-		}
304
-
305
-		if ($level >= $minLevel) {
306
-			$logger = $this->logger;
307
-			call_user_func(array($logger, 'write'), $app, $message, $level);
308
-		}
309
-	}
310
-
311
-	/**
312
-	 * Logs an exception very detailed
313
-	 *
314
-	 * @param \Exception|\Throwable $exception
315
-	 * @param array $context
316
-	 * @return void
317
-	 * @since 8.2.0
318
-	 */
319
-	public function logException($exception, array $context = array()) {
320
-		$level = Util::ERROR;
321
-		if (isset($context['level'])) {
322
-			$level = $context['level'];
323
-			unset($context['level']);
324
-		}
325
-		$data = array(
326
-			'Exception' => get_class($exception),
327
-			'Message' => $exception->getMessage(),
328
-			'Code' => $exception->getCode(),
329
-			'Trace' => $exception->getTraceAsString(),
330
-			'File' => $exception->getFile(),
331
-			'Line' => $exception->getLine(),
332
-		);
333
-		$data['Trace'] = preg_replace('!(' . implode('|', $this->methodsWithSensitiveParameters) . ')\(.*\)!', '$1(*** sensitive parameters replaced ***)', $data['Trace']);
334
-		$msg = isset($context['message']) ? $context['message'] : 'Exception';
335
-		$msg .= ': ' . json_encode($data);
336
-		$this->log($level, $msg, $context);
337
-	}
338
-
339
-	/**
340
-	 * @param string $logType
341
-	 * @return string
342
-	 * @internal
343
-	 */
344
-	public static function getLogClass($logType) {
345
-		switch (strtolower($logType)) {
346
-			case 'errorlog':
347
-				return \OC\Log\Errorlog::class;
348
-			case 'syslog':
349
-				return \OC\Log\Syslog::class;
350
-			case 'file':
351
-				return \OC\Log\File::class;
352
-
353
-			// Backwards compatibility for old and fallback for unknown log types
354
-			case 'owncloud':
355
-			case 'nextcloud':
356
-			default:
357
-				return \OC\Log\File::class;
358
-		}
359
-	}
51
+    /** @var string */
52
+    private $logger;
53
+
54
+    /** @var SystemConfig */
55
+    private $config;
56
+
57
+    /** @var boolean|null cache the result of the log condition check for the request */
58
+    private $logConditionSatisfied = null;
59
+
60
+    /** @var Normalizer */
61
+    private $normalizer;
62
+
63
+    protected $methodsWithSensitiveParameters = [
64
+        // Session/User
65
+        'completeLogin',
66
+        'login',
67
+        'checkPassword',
68
+        'loginWithPassword',
69
+        'updatePrivateKeyPassword',
70
+        'validateUserPass',
71
+
72
+        // TokenProvider
73
+        'getToken',
74
+        'isTokenPassword',
75
+        'getPassword',
76
+        'decryptPassword',
77
+        'logClientIn',
78
+        'generateToken',
79
+        'validateToken',
80
+
81
+        // TwoFactorAuth
82
+        'solveChallenge',
83
+        'verifyChallenge',
84
+
85
+        // ICrypto
86
+        'calculateHMAC',
87
+        'encrypt',
88
+        'decrypt',
89
+
90
+        // LoginController
91
+        'tryLogin',
92
+        'confirmPassword',
93
+
94
+        // LDAP
95
+        'bind',
96
+        'areCredentialsValid',
97
+        'invokeLDAPMethod',
98
+        'checkPasswordNoLogging',
99
+    ];
100
+
101
+    /**
102
+     * @param string $logger The logger that should be used
103
+     * @param SystemConfig $config the system config object
104
+     * @param null $normalizer
105
+     */
106
+    public function __construct($logger = null, SystemConfig $config = null, $normalizer = null) {
107
+        // FIXME: Add this for backwards compatibility, should be fixed at some point probably
108
+        if($config === null) {
109
+            $config = \OC::$server->getSystemConfig();
110
+        }
111
+
112
+        $this->config = $config;
113
+
114
+        // FIXME: Add this for backwards compatibility, should be fixed at some point probably
115
+        if($logger === null) {
116
+            $logType = $this->config->getValue('log_type', 'file');
117
+            $this->logger = static::getLogClass($logType);
118
+            call_user_func(array($this->logger, 'init'));
119
+        } else {
120
+            $this->logger = $logger;
121
+        }
122
+        if ($normalizer === null) {
123
+            $this->normalizer = new Normalizer();
124
+        } else {
125
+            $this->normalizer = $normalizer;
126
+        }
127
+
128
+    }
129
+
130
+    /**
131
+     * System is unusable.
132
+     *
133
+     * @param string $message
134
+     * @param array $context
135
+     * @return void
136
+     */
137
+    public function emergency($message, array $context = array()) {
138
+        $this->log(Util::FATAL, $message, $context);
139
+    }
140
+
141
+    /**
142
+     * Action must be taken immediately.
143
+     *
144
+     * Example: Entire website down, database unavailable, etc. This should
145
+     * trigger the SMS alerts and wake you up.
146
+     *
147
+     * @param string $message
148
+     * @param array $context
149
+     * @return void
150
+     */
151
+    public function alert($message, array $context = array()) {
152
+        $this->log(Util::ERROR, $message, $context);
153
+    }
154
+
155
+    /**
156
+     * Critical conditions.
157
+     *
158
+     * Example: Application component unavailable, unexpected exception.
159
+     *
160
+     * @param string $message
161
+     * @param array $context
162
+     * @return void
163
+     */
164
+    public function critical($message, array $context = array()) {
165
+        $this->log(Util::ERROR, $message, $context);
166
+    }
167
+
168
+    /**
169
+     * Runtime errors that do not require immediate action but should typically
170
+     * be logged and monitored.
171
+     *
172
+     * @param string $message
173
+     * @param array $context
174
+     * @return void
175
+     */
176
+    public function error($message, array $context = array()) {
177
+        $this->log(Util::ERROR, $message, $context);
178
+    }
179
+
180
+    /**
181
+     * Exceptional occurrences that are not errors.
182
+     *
183
+     * Example: Use of deprecated APIs, poor use of an API, undesirable things
184
+     * that are not necessarily wrong.
185
+     *
186
+     * @param string $message
187
+     * @param array $context
188
+     * @return void
189
+     */
190
+    public function warning($message, array $context = array()) {
191
+        $this->log(Util::WARN, $message, $context);
192
+    }
193
+
194
+    /**
195
+     * Normal but significant events.
196
+     *
197
+     * @param string $message
198
+     * @param array $context
199
+     * @return void
200
+     */
201
+    public function notice($message, array $context = array()) {
202
+        $this->log(Util::INFO, $message, $context);
203
+    }
204
+
205
+    /**
206
+     * Interesting events.
207
+     *
208
+     * Example: User logs in, SQL logs.
209
+     *
210
+     * @param string $message
211
+     * @param array $context
212
+     * @return void
213
+     */
214
+    public function info($message, array $context = array()) {
215
+        $this->log(Util::INFO, $message, $context);
216
+    }
217
+
218
+    /**
219
+     * Detailed debug information.
220
+     *
221
+     * @param string $message
222
+     * @param array $context
223
+     * @return void
224
+     */
225
+    public function debug($message, array $context = array()) {
226
+        $this->log(Util::DEBUG, $message, $context);
227
+    }
228
+
229
+
230
+    /**
231
+     * Logs with an arbitrary level.
232
+     *
233
+     * @param mixed $level
234
+     * @param string $message
235
+     * @param array $context
236
+     * @return void
237
+     */
238
+    public function log($level, $message, array $context = array()) {
239
+        $minLevel = min($this->config->getValue('loglevel', Util::WARN), Util::FATAL);
240
+        $logCondition = $this->config->getValue('log.condition', []);
241
+
242
+        array_walk($context, [$this->normalizer, 'format']);
243
+
244
+        if (isset($context['app'])) {
245
+            $app = $context['app'];
246
+
247
+            /**
248
+             * check log condition based on the context of each log message
249
+             * once this is met -> change the required log level to debug
250
+             */
251
+            if(!empty($logCondition)
252
+                && isset($logCondition['apps'])
253
+                && in_array($app, $logCondition['apps'], true)) {
254
+                $minLevel = Util::DEBUG;
255
+            }
256
+
257
+        } else {
258
+            $app = 'no app in context';
259
+        }
260
+        // interpolate $message as defined in PSR-3
261
+        $replace = array();
262
+        foreach ($context as $key => $val) {
263
+            $replace['{' . $key . '}'] = $val;
264
+        }
265
+
266
+        // interpolate replacement values into the message and return
267
+        $message = strtr($message, $replace);
268
+
269
+        /**
270
+         * check for a special log condition - this enables an increased log on
271
+         * a per request/user base
272
+         */
273
+        if($this->logConditionSatisfied === null) {
274
+            // default to false to just process this once per request
275
+            $this->logConditionSatisfied = false;
276
+            if(!empty($logCondition)) {
277
+
278
+                // check for secret token in the request
279
+                if(isset($logCondition['shared_secret'])) {
280
+                    $request = \OC::$server->getRequest();
281
+
282
+                    // if token is found in the request change set the log condition to satisfied
283
+                    if($request && hash_equals($logCondition['shared_secret'], $request->getParam('log_secret', ''))) {
284
+                        $this->logConditionSatisfied = true;
285
+                    }
286
+                }
287
+
288
+                // check for user
289
+                if(isset($logCondition['users'])) {
290
+                    $user = \OC::$server->getUserSession()->getUser();
291
+
292
+                    // if the user matches set the log condition to satisfied
293
+                    if($user !== null && in_array($user->getUID(), $logCondition['users'], true)) {
294
+                        $this->logConditionSatisfied = true;
295
+                    }
296
+                }
297
+            }
298
+        }
299
+
300
+        // if log condition is satisfied change the required log level to DEBUG
301
+        if($this->logConditionSatisfied) {
302
+            $minLevel = Util::DEBUG;
303
+        }
304
+
305
+        if ($level >= $minLevel) {
306
+            $logger = $this->logger;
307
+            call_user_func(array($logger, 'write'), $app, $message, $level);
308
+        }
309
+    }
310
+
311
+    /**
312
+     * Logs an exception very detailed
313
+     *
314
+     * @param \Exception|\Throwable $exception
315
+     * @param array $context
316
+     * @return void
317
+     * @since 8.2.0
318
+     */
319
+    public function logException($exception, array $context = array()) {
320
+        $level = Util::ERROR;
321
+        if (isset($context['level'])) {
322
+            $level = $context['level'];
323
+            unset($context['level']);
324
+        }
325
+        $data = array(
326
+            'Exception' => get_class($exception),
327
+            'Message' => $exception->getMessage(),
328
+            'Code' => $exception->getCode(),
329
+            'Trace' => $exception->getTraceAsString(),
330
+            'File' => $exception->getFile(),
331
+            'Line' => $exception->getLine(),
332
+        );
333
+        $data['Trace'] = preg_replace('!(' . implode('|', $this->methodsWithSensitiveParameters) . ')\(.*\)!', '$1(*** sensitive parameters replaced ***)', $data['Trace']);
334
+        $msg = isset($context['message']) ? $context['message'] : 'Exception';
335
+        $msg .= ': ' . json_encode($data);
336
+        $this->log($level, $msg, $context);
337
+    }
338
+
339
+    /**
340
+     * @param string $logType
341
+     * @return string
342
+     * @internal
343
+     */
344
+    public static function getLogClass($logType) {
345
+        switch (strtolower($logType)) {
346
+            case 'errorlog':
347
+                return \OC\Log\Errorlog::class;
348
+            case 'syslog':
349
+                return \OC\Log\Syslog::class;
350
+            case 'file':
351
+                return \OC\Log\File::class;
352
+
353
+            // Backwards compatibility for old and fallback for unknown log types
354
+            case 'owncloud':
355
+            case 'nextcloud':
356
+            default:
357
+                return \OC\Log\File::class;
358
+        }
359
+    }
360 360
 }
Please login to merge, or discard this patch.
Spacing   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -105,14 +105,14 @@  discard block
 block discarded – undo
105 105
 	 */
106 106
 	public function __construct($logger = null, SystemConfig $config = null, $normalizer = null) {
107 107
 		// FIXME: Add this for backwards compatibility, should be fixed at some point probably
108
-		if($config === null) {
108
+		if ($config === null) {
109 109
 			$config = \OC::$server->getSystemConfig();
110 110
 		}
111 111
 
112 112
 		$this->config = $config;
113 113
 
114 114
 		// FIXME: Add this for backwards compatibility, should be fixed at some point probably
115
-		if($logger === null) {
115
+		if ($logger === null) {
116 116
 			$logType = $this->config->getValue('log_type', 'file');
117 117
 			$this->logger = static::getLogClass($logType);
118 118
 			call_user_func(array($this->logger, 'init'));
@@ -248,7 +248,7 @@  discard block
 block discarded – undo
248 248
 			 * check log condition based on the context of each log message
249 249
 			 * once this is met -> change the required log level to debug
250 250
 			 */
251
-			if(!empty($logCondition)
251
+			if (!empty($logCondition)
252 252
 				&& isset($logCondition['apps'])
253 253
 				&& in_array($app, $logCondition['apps'], true)) {
254 254
 				$minLevel = Util::DEBUG;
@@ -260,7 +260,7 @@  discard block
 block discarded – undo
260 260
 		// interpolate $message as defined in PSR-3
261 261
 		$replace = array();
262 262
 		foreach ($context as $key => $val) {
263
-			$replace['{' . $key . '}'] = $val;
263
+			$replace['{'.$key.'}'] = $val;
264 264
 		}
265 265
 
266 266
 		// interpolate replacement values into the message and return
@@ -270,27 +270,27 @@  discard block
 block discarded – undo
270 270
 		 * check for a special log condition - this enables an increased log on
271 271
 		 * a per request/user base
272 272
 		 */
273
-		if($this->logConditionSatisfied === null) {
273
+		if ($this->logConditionSatisfied === null) {
274 274
 			// default to false to just process this once per request
275 275
 			$this->logConditionSatisfied = false;
276
-			if(!empty($logCondition)) {
276
+			if (!empty($logCondition)) {
277 277
 
278 278
 				// check for secret token in the request
279
-				if(isset($logCondition['shared_secret'])) {
279
+				if (isset($logCondition['shared_secret'])) {
280 280
 					$request = \OC::$server->getRequest();
281 281
 
282 282
 					// if token is found in the request change set the log condition to satisfied
283
-					if($request && hash_equals($logCondition['shared_secret'], $request->getParam('log_secret', ''))) {
283
+					if ($request && hash_equals($logCondition['shared_secret'], $request->getParam('log_secret', ''))) {
284 284
 						$this->logConditionSatisfied = true;
285 285
 					}
286 286
 				}
287 287
 
288 288
 				// check for user
289
-				if(isset($logCondition['users'])) {
289
+				if (isset($logCondition['users'])) {
290 290
 					$user = \OC::$server->getUserSession()->getUser();
291 291
 
292 292
 					// if the user matches set the log condition to satisfied
293
-					if($user !== null && in_array($user->getUID(), $logCondition['users'], true)) {
293
+					if ($user !== null && in_array($user->getUID(), $logCondition['users'], true)) {
294 294
 						$this->logConditionSatisfied = true;
295 295
 					}
296 296
 				}
@@ -298,7 +298,7 @@  discard block
 block discarded – undo
298 298
 		}
299 299
 
300 300
 		// if log condition is satisfied change the required log level to DEBUG
301
-		if($this->logConditionSatisfied) {
301
+		if ($this->logConditionSatisfied) {
302 302
 			$minLevel = Util::DEBUG;
303 303
 		}
304 304
 
@@ -330,9 +330,9 @@  discard block
 block discarded – undo
330 330
 			'File' => $exception->getFile(),
331 331
 			'Line' => $exception->getLine(),
332 332
 		);
333
-		$data['Trace'] = preg_replace('!(' . implode('|', $this->methodsWithSensitiveParameters) . ')\(.*\)!', '$1(*** sensitive parameters replaced ***)', $data['Trace']);
333
+		$data['Trace'] = preg_replace('!('.implode('|', $this->methodsWithSensitiveParameters).')\(.*\)!', '$1(*** sensitive parameters replaced ***)', $data['Trace']);
334 334
 		$msg = isset($context['message']) ? $context['message'] : 'Exception';
335
-		$msg .= ': ' . json_encode($data);
335
+		$msg .= ': '.json_encode($data);
336 336
 		$this->log($level, $msg, $context);
337 337
 	}
338 338
 
Please login to merge, or discard this patch.