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