@@ -48,307 +48,307 @@ |
||
| 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 | - |
|
| 95 | - /** |
|
| 96 | - * @param string $logger The logger that should be used |
|
| 97 | - * @param SystemConfig $config the system config object |
|
| 98 | - * @param null $normalizer |
|
| 99 | - */ |
|
| 100 | - public function __construct($logger=null, SystemConfig $config=null, $normalizer = null) { |
|
| 101 | - // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
|
| 102 | - if($config === null) { |
|
| 103 | - $config = \OC::$server->getSystemConfig(); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - $this->config = $config; |
|
| 107 | - |
|
| 108 | - // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
|
| 109 | - if($logger === null) { |
|
| 110 | - $logType = $this->config->getValue('log_type', 'file'); |
|
| 111 | - $this->logger = static::getLogClass($logType); |
|
| 112 | - call_user_func(array($this->logger, 'init')); |
|
| 113 | - } else { |
|
| 114 | - $this->logger = $logger; |
|
| 115 | - } |
|
| 116 | - if ($normalizer === null) { |
|
| 117 | - $this->normalizer = new Normalizer(); |
|
| 118 | - } else { |
|
| 119 | - $this->normalizer = $normalizer; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * System is unusable. |
|
| 126 | - * |
|
| 127 | - * @param string $message |
|
| 128 | - * @param array $context |
|
| 129 | - * @return void |
|
| 130 | - */ |
|
| 131 | - public function emergency($message, array $context = array()) { |
|
| 132 | - $this->log(Util::FATAL, $message, $context); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Action must be taken immediately. |
|
| 137 | - * |
|
| 138 | - * Example: Entire website down, database unavailable, etc. This should |
|
| 139 | - * trigger the SMS alerts and wake you up. |
|
| 140 | - * |
|
| 141 | - * @param string $message |
|
| 142 | - * @param array $context |
|
| 143 | - * @return void |
|
| 144 | - */ |
|
| 145 | - public function alert($message, array $context = array()) { |
|
| 146 | - $this->log(Util::ERROR, $message, $context); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Critical conditions. |
|
| 151 | - * |
|
| 152 | - * Example: Application component unavailable, unexpected exception. |
|
| 153 | - * |
|
| 154 | - * @param string $message |
|
| 155 | - * @param array $context |
|
| 156 | - * @return void |
|
| 157 | - */ |
|
| 158 | - public function critical($message, array $context = array()) { |
|
| 159 | - $this->log(Util::ERROR, $message, $context); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Runtime errors that do not require immediate action but should typically |
|
| 164 | - * be logged and monitored. |
|
| 165 | - * |
|
| 166 | - * @param string $message |
|
| 167 | - * @param array $context |
|
| 168 | - * @return void |
|
| 169 | - */ |
|
| 170 | - public function error($message, array $context = array()) { |
|
| 171 | - $this->log(Util::ERROR, $message, $context); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Exceptional occurrences that are not errors. |
|
| 176 | - * |
|
| 177 | - * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
| 178 | - * that are not necessarily wrong. |
|
| 179 | - * |
|
| 180 | - * @param string $message |
|
| 181 | - * @param array $context |
|
| 182 | - * @return void |
|
| 183 | - */ |
|
| 184 | - public function warning($message, array $context = array()) { |
|
| 185 | - $this->log(Util::WARN, $message, $context); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Normal but significant events. |
|
| 190 | - * |
|
| 191 | - * @param string $message |
|
| 192 | - * @param array $context |
|
| 193 | - * @return void |
|
| 194 | - */ |
|
| 195 | - public function notice($message, array $context = array()) { |
|
| 196 | - $this->log(Util::INFO, $message, $context); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Interesting events. |
|
| 201 | - * |
|
| 202 | - * Example: User logs in, SQL logs. |
|
| 203 | - * |
|
| 204 | - * @param string $message |
|
| 205 | - * @param array $context |
|
| 206 | - * @return void |
|
| 207 | - */ |
|
| 208 | - public function info($message, array $context = array()) { |
|
| 209 | - $this->log(Util::INFO, $message, $context); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Detailed debug information. |
|
| 214 | - * |
|
| 215 | - * @param string $message |
|
| 216 | - * @param array $context |
|
| 217 | - * @return void |
|
| 218 | - */ |
|
| 219 | - public function debug($message, array $context = array()) { |
|
| 220 | - $this->log(Util::DEBUG, $message, $context); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Logs with an arbitrary level. |
|
| 226 | - * |
|
| 227 | - * @param mixed $level |
|
| 228 | - * @param string $message |
|
| 229 | - * @param array $context |
|
| 230 | - * @return void |
|
| 231 | - */ |
|
| 232 | - public function log($level, $message, array $context = array()) { |
|
| 233 | - $minLevel = min($this->config->getValue('loglevel', Util::WARN), Util::FATAL); |
|
| 234 | - $logCondition = $this->config->getValue('log.condition', []); |
|
| 235 | - |
|
| 236 | - array_walk($context, [$this->normalizer, 'format']); |
|
| 237 | - |
|
| 238 | - if (isset($context['app'])) { |
|
| 239 | - $app = $context['app']; |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * check log condition based on the context of each log message |
|
| 243 | - * once this is met -> change the required log level to debug |
|
| 244 | - */ |
|
| 245 | - if(!empty($logCondition) |
|
| 246 | - && isset($logCondition['apps']) |
|
| 247 | - && in_array($app, $logCondition['apps'], true)) { |
|
| 248 | - $minLevel = Util::DEBUG; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - } else { |
|
| 252 | - $app = 'no app in context'; |
|
| 253 | - } |
|
| 254 | - // interpolate $message as defined in PSR-3 |
|
| 255 | - $replace = array(); |
|
| 256 | - foreach ($context as $key => $val) { |
|
| 257 | - $replace['{' . $key . '}'] = $val; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - // interpolate replacement values into the message and return |
|
| 261 | - $message = strtr($message, $replace); |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * check for a special log condition - this enables an increased log on |
|
| 265 | - * a per request/user base |
|
| 266 | - */ |
|
| 267 | - if($this->logConditionSatisfied === null) { |
|
| 268 | - // default to false to just process this once per request |
|
| 269 | - $this->logConditionSatisfied = false; |
|
| 270 | - if(!empty($logCondition)) { |
|
| 271 | - |
|
| 272 | - // check for secret token in the request |
|
| 273 | - if(isset($logCondition['shared_secret'])) { |
|
| 274 | - $request = \OC::$server->getRequest(); |
|
| 275 | - |
|
| 276 | - // if token is found in the request change set the log condition to satisfied |
|
| 277 | - if($request && hash_equals($logCondition['shared_secret'], $request->getParam('log_secret', ''))) { |
|
| 278 | - $this->logConditionSatisfied = true; |
|
| 279 | - } |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - // check for user |
|
| 283 | - if(isset($logCondition['users'])) { |
|
| 284 | - $user = \OC::$server->getUserSession()->getUser(); |
|
| 285 | - |
|
| 286 | - // if the user matches set the log condition to satisfied |
|
| 287 | - if($user !== null && in_array($user->getUID(), $logCondition['users'], true)) { |
|
| 288 | - $this->logConditionSatisfied = true; |
|
| 289 | - } |
|
| 290 | - } |
|
| 291 | - } |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - // if log condition is satisfied change the required log level to DEBUG |
|
| 295 | - if($this->logConditionSatisfied) { |
|
| 296 | - $minLevel = Util::DEBUG; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - if ($level >= $minLevel) { |
|
| 300 | - $logger = $this->logger; |
|
| 301 | - call_user_func(array($logger, 'write'), $app, $message, $level); |
|
| 302 | - } |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Logs an exception very detailed |
|
| 307 | - * |
|
| 308 | - * @param \Exception|\Throwable $exception |
|
| 309 | - * @param array $context |
|
| 310 | - * @return void |
|
| 311 | - * @since 8.2.0 |
|
| 312 | - */ |
|
| 313 | - public function logException($exception, array $context = array()) { |
|
| 314 | - $level = Util::ERROR; |
|
| 315 | - if (isset($context['level'])) { |
|
| 316 | - $level = $context['level']; |
|
| 317 | - unset($context['level']); |
|
| 318 | - } |
|
| 319 | - $data = array( |
|
| 320 | - 'Exception' => get_class($exception), |
|
| 321 | - 'Message' => $exception->getMessage(), |
|
| 322 | - 'Code' => $exception->getCode(), |
|
| 323 | - 'Trace' => $exception->getTraceAsString(), |
|
| 324 | - 'File' => $exception->getFile(), |
|
| 325 | - 'Line' => $exception->getLine(), |
|
| 326 | - ); |
|
| 327 | - $data['Trace'] = preg_replace('!(' . implode('|', $this->methodsWithSensitiveParameters) . ')\(.*\)!', '$1(*** sensitive parameters replaced ***)', $data['Trace']); |
|
| 328 | - $msg = isset($context['message']) ? $context['message'] : 'Exception'; |
|
| 329 | - $msg .= ': ' . json_encode($data); |
|
| 330 | - $this->log($level, $msg, $context); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * @param string $logType |
|
| 335 | - * @return string |
|
| 336 | - * @internal |
|
| 337 | - */ |
|
| 338 | - public static function getLogClass($logType) { |
|
| 339 | - switch (strtolower($logType)) { |
|
| 340 | - case 'errorlog': |
|
| 341 | - return \OC\Log\Errorlog::class; |
|
| 342 | - case 'syslog': |
|
| 343 | - return \OC\Log\Syslog::class; |
|
| 344 | - case 'file': |
|
| 345 | - return \OC\Log\File::class; |
|
| 346 | - |
|
| 347 | - // Backwards compatibility for old and fallback for unknown log types |
|
| 348 | - case 'owncloud': |
|
| 349 | - case 'nextcloud': |
|
| 350 | - default: |
|
| 351 | - return \OC\Log\File::class; |
|
| 352 | - } |
|
| 353 | - } |
|
| 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 | + |
|
| 95 | + /** |
|
| 96 | + * @param string $logger The logger that should be used |
|
| 97 | + * @param SystemConfig $config the system config object |
|
| 98 | + * @param null $normalizer |
|
| 99 | + */ |
|
| 100 | + public function __construct($logger=null, SystemConfig $config=null, $normalizer = null) { |
|
| 101 | + // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
|
| 102 | + if($config === null) { |
|
| 103 | + $config = \OC::$server->getSystemConfig(); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + $this->config = $config; |
|
| 107 | + |
|
| 108 | + // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
|
| 109 | + if($logger === null) { |
|
| 110 | + $logType = $this->config->getValue('log_type', 'file'); |
|
| 111 | + $this->logger = static::getLogClass($logType); |
|
| 112 | + call_user_func(array($this->logger, 'init')); |
|
| 113 | + } else { |
|
| 114 | + $this->logger = $logger; |
|
| 115 | + } |
|
| 116 | + if ($normalizer === null) { |
|
| 117 | + $this->normalizer = new Normalizer(); |
|
| 118 | + } else { |
|
| 119 | + $this->normalizer = $normalizer; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * System is unusable. |
|
| 126 | + * |
|
| 127 | + * @param string $message |
|
| 128 | + * @param array $context |
|
| 129 | + * @return void |
|
| 130 | + */ |
|
| 131 | + public function emergency($message, array $context = array()) { |
|
| 132 | + $this->log(Util::FATAL, $message, $context); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Action must be taken immediately. |
|
| 137 | + * |
|
| 138 | + * Example: Entire website down, database unavailable, etc. This should |
|
| 139 | + * trigger the SMS alerts and wake you up. |
|
| 140 | + * |
|
| 141 | + * @param string $message |
|
| 142 | + * @param array $context |
|
| 143 | + * @return void |
|
| 144 | + */ |
|
| 145 | + public function alert($message, array $context = array()) { |
|
| 146 | + $this->log(Util::ERROR, $message, $context); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Critical conditions. |
|
| 151 | + * |
|
| 152 | + * Example: Application component unavailable, unexpected exception. |
|
| 153 | + * |
|
| 154 | + * @param string $message |
|
| 155 | + * @param array $context |
|
| 156 | + * @return void |
|
| 157 | + */ |
|
| 158 | + public function critical($message, array $context = array()) { |
|
| 159 | + $this->log(Util::ERROR, $message, $context); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Runtime errors that do not require immediate action but should typically |
|
| 164 | + * be logged and monitored. |
|
| 165 | + * |
|
| 166 | + * @param string $message |
|
| 167 | + * @param array $context |
|
| 168 | + * @return void |
|
| 169 | + */ |
|
| 170 | + public function error($message, array $context = array()) { |
|
| 171 | + $this->log(Util::ERROR, $message, $context); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Exceptional occurrences that are not errors. |
|
| 176 | + * |
|
| 177 | + * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
| 178 | + * that are not necessarily wrong. |
|
| 179 | + * |
|
| 180 | + * @param string $message |
|
| 181 | + * @param array $context |
|
| 182 | + * @return void |
|
| 183 | + */ |
|
| 184 | + public function warning($message, array $context = array()) { |
|
| 185 | + $this->log(Util::WARN, $message, $context); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Normal but significant events. |
|
| 190 | + * |
|
| 191 | + * @param string $message |
|
| 192 | + * @param array $context |
|
| 193 | + * @return void |
|
| 194 | + */ |
|
| 195 | + public function notice($message, array $context = array()) { |
|
| 196 | + $this->log(Util::INFO, $message, $context); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Interesting events. |
|
| 201 | + * |
|
| 202 | + * Example: User logs in, SQL logs. |
|
| 203 | + * |
|
| 204 | + * @param string $message |
|
| 205 | + * @param array $context |
|
| 206 | + * @return void |
|
| 207 | + */ |
|
| 208 | + public function info($message, array $context = array()) { |
|
| 209 | + $this->log(Util::INFO, $message, $context); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Detailed debug information. |
|
| 214 | + * |
|
| 215 | + * @param string $message |
|
| 216 | + * @param array $context |
|
| 217 | + * @return void |
|
| 218 | + */ |
|
| 219 | + public function debug($message, array $context = array()) { |
|
| 220 | + $this->log(Util::DEBUG, $message, $context); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Logs with an arbitrary level. |
|
| 226 | + * |
|
| 227 | + * @param mixed $level |
|
| 228 | + * @param string $message |
|
| 229 | + * @param array $context |
|
| 230 | + * @return void |
|
| 231 | + */ |
|
| 232 | + public function log($level, $message, array $context = array()) { |
|
| 233 | + $minLevel = min($this->config->getValue('loglevel', Util::WARN), Util::FATAL); |
|
| 234 | + $logCondition = $this->config->getValue('log.condition', []); |
|
| 235 | + |
|
| 236 | + array_walk($context, [$this->normalizer, 'format']); |
|
| 237 | + |
|
| 238 | + if (isset($context['app'])) { |
|
| 239 | + $app = $context['app']; |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * check log condition based on the context of each log message |
|
| 243 | + * once this is met -> change the required log level to debug |
|
| 244 | + */ |
|
| 245 | + if(!empty($logCondition) |
|
| 246 | + && isset($logCondition['apps']) |
|
| 247 | + && in_array($app, $logCondition['apps'], true)) { |
|
| 248 | + $minLevel = Util::DEBUG; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + } else { |
|
| 252 | + $app = 'no app in context'; |
|
| 253 | + } |
|
| 254 | + // interpolate $message as defined in PSR-3 |
|
| 255 | + $replace = array(); |
|
| 256 | + foreach ($context as $key => $val) { |
|
| 257 | + $replace['{' . $key . '}'] = $val; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + // interpolate replacement values into the message and return |
|
| 261 | + $message = strtr($message, $replace); |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * check for a special log condition - this enables an increased log on |
|
| 265 | + * a per request/user base |
|
| 266 | + */ |
|
| 267 | + if($this->logConditionSatisfied === null) { |
|
| 268 | + // default to false to just process this once per request |
|
| 269 | + $this->logConditionSatisfied = false; |
|
| 270 | + if(!empty($logCondition)) { |
|
| 271 | + |
|
| 272 | + // check for secret token in the request |
|
| 273 | + if(isset($logCondition['shared_secret'])) { |
|
| 274 | + $request = \OC::$server->getRequest(); |
|
| 275 | + |
|
| 276 | + // if token is found in the request change set the log condition to satisfied |
|
| 277 | + if($request && hash_equals($logCondition['shared_secret'], $request->getParam('log_secret', ''))) { |
|
| 278 | + $this->logConditionSatisfied = true; |
|
| 279 | + } |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + // check for user |
|
| 283 | + if(isset($logCondition['users'])) { |
|
| 284 | + $user = \OC::$server->getUserSession()->getUser(); |
|
| 285 | + |
|
| 286 | + // if the user matches set the log condition to satisfied |
|
| 287 | + if($user !== null && in_array($user->getUID(), $logCondition['users'], true)) { |
|
| 288 | + $this->logConditionSatisfied = true; |
|
| 289 | + } |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + // if log condition is satisfied change the required log level to DEBUG |
|
| 295 | + if($this->logConditionSatisfied) { |
|
| 296 | + $minLevel = Util::DEBUG; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + if ($level >= $minLevel) { |
|
| 300 | + $logger = $this->logger; |
|
| 301 | + call_user_func(array($logger, 'write'), $app, $message, $level); |
|
| 302 | + } |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Logs an exception very detailed |
|
| 307 | + * |
|
| 308 | + * @param \Exception|\Throwable $exception |
|
| 309 | + * @param array $context |
|
| 310 | + * @return void |
|
| 311 | + * @since 8.2.0 |
|
| 312 | + */ |
|
| 313 | + public function logException($exception, array $context = array()) { |
|
| 314 | + $level = Util::ERROR; |
|
| 315 | + if (isset($context['level'])) { |
|
| 316 | + $level = $context['level']; |
|
| 317 | + unset($context['level']); |
|
| 318 | + } |
|
| 319 | + $data = array( |
|
| 320 | + 'Exception' => get_class($exception), |
|
| 321 | + 'Message' => $exception->getMessage(), |
|
| 322 | + 'Code' => $exception->getCode(), |
|
| 323 | + 'Trace' => $exception->getTraceAsString(), |
|
| 324 | + 'File' => $exception->getFile(), |
|
| 325 | + 'Line' => $exception->getLine(), |
|
| 326 | + ); |
|
| 327 | + $data['Trace'] = preg_replace('!(' . implode('|', $this->methodsWithSensitiveParameters) . ')\(.*\)!', '$1(*** sensitive parameters replaced ***)', $data['Trace']); |
|
| 328 | + $msg = isset($context['message']) ? $context['message'] : 'Exception'; |
|
| 329 | + $msg .= ': ' . json_encode($data); |
|
| 330 | + $this->log($level, $msg, $context); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * @param string $logType |
|
| 335 | + * @return string |
|
| 336 | + * @internal |
|
| 337 | + */ |
|
| 338 | + public static function getLogClass($logType) { |
|
| 339 | + switch (strtolower($logType)) { |
|
| 340 | + case 'errorlog': |
|
| 341 | + return \OC\Log\Errorlog::class; |
|
| 342 | + case 'syslog': |
|
| 343 | + return \OC\Log\Syslog::class; |
|
| 344 | + case 'file': |
|
| 345 | + return \OC\Log\File::class; |
|
| 346 | + |
|
| 347 | + // Backwards compatibility for old and fallback for unknown log types |
|
| 348 | + case 'owncloud': |
|
| 349 | + case 'nextcloud': |
|
| 350 | + default: |
|
| 351 | + return \OC\Log\File::class; |
|
| 352 | + } |
|
| 353 | + } |
|
| 354 | 354 | } |
@@ -97,16 +97,16 @@ discard block |
||
| 97 | 97 | * @param SystemConfig $config the system config object |
| 98 | 98 | * @param null $normalizer |
| 99 | 99 | */ |
| 100 | - public function __construct($logger=null, SystemConfig $config=null, $normalizer = null) { |
|
| 100 | + public function __construct($logger = null, SystemConfig $config = null, $normalizer = null) { |
|
| 101 | 101 | // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
| 102 | - if($config === null) { |
|
| 102 | + if ($config === null) { |
|
| 103 | 103 | $config = \OC::$server->getSystemConfig(); |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | $this->config = $config; |
| 107 | 107 | |
| 108 | 108 | // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
| 109 | - if($logger === null) { |
|
| 109 | + if ($logger === null) { |
|
| 110 | 110 | $logType = $this->config->getValue('log_type', 'file'); |
| 111 | 111 | $this->logger = static::getLogClass($logType); |
| 112 | 112 | call_user_func(array($this->logger, 'init')); |
@@ -242,7 +242,7 @@ discard block |
||
| 242 | 242 | * check log condition based on the context of each log message |
| 243 | 243 | * once this is met -> change the required log level to debug |
| 244 | 244 | */ |
| 245 | - if(!empty($logCondition) |
|
| 245 | + if (!empty($logCondition) |
|
| 246 | 246 | && isset($logCondition['apps']) |
| 247 | 247 | && in_array($app, $logCondition['apps'], true)) { |
| 248 | 248 | $minLevel = Util::DEBUG; |
@@ -254,7 +254,7 @@ discard block |
||
| 254 | 254 | // interpolate $message as defined in PSR-3 |
| 255 | 255 | $replace = array(); |
| 256 | 256 | foreach ($context as $key => $val) { |
| 257 | - $replace['{' . $key . '}'] = $val; |
|
| 257 | + $replace['{'.$key.'}'] = $val; |
|
| 258 | 258 | } |
| 259 | 259 | |
| 260 | 260 | // interpolate replacement values into the message and return |
@@ -264,27 +264,27 @@ discard block |
||
| 264 | 264 | * check for a special log condition - this enables an increased log on |
| 265 | 265 | * a per request/user base |
| 266 | 266 | */ |
| 267 | - if($this->logConditionSatisfied === null) { |
|
| 267 | + if ($this->logConditionSatisfied === null) { |
|
| 268 | 268 | // default to false to just process this once per request |
| 269 | 269 | $this->logConditionSatisfied = false; |
| 270 | - if(!empty($logCondition)) { |
|
| 270 | + if (!empty($logCondition)) { |
|
| 271 | 271 | |
| 272 | 272 | // check for secret token in the request |
| 273 | - if(isset($logCondition['shared_secret'])) { |
|
| 273 | + if (isset($logCondition['shared_secret'])) { |
|
| 274 | 274 | $request = \OC::$server->getRequest(); |
| 275 | 275 | |
| 276 | 276 | // if token is found in the request change set the log condition to satisfied |
| 277 | - if($request && hash_equals($logCondition['shared_secret'], $request->getParam('log_secret', ''))) { |
|
| 277 | + if ($request && hash_equals($logCondition['shared_secret'], $request->getParam('log_secret', ''))) { |
|
| 278 | 278 | $this->logConditionSatisfied = true; |
| 279 | 279 | } |
| 280 | 280 | } |
| 281 | 281 | |
| 282 | 282 | // check for user |
| 283 | - if(isset($logCondition['users'])) { |
|
| 283 | + if (isset($logCondition['users'])) { |
|
| 284 | 284 | $user = \OC::$server->getUserSession()->getUser(); |
| 285 | 285 | |
| 286 | 286 | // if the user matches set the log condition to satisfied |
| 287 | - if($user !== null && in_array($user->getUID(), $logCondition['users'], true)) { |
|
| 287 | + if ($user !== null && in_array($user->getUID(), $logCondition['users'], true)) { |
|
| 288 | 288 | $this->logConditionSatisfied = true; |
| 289 | 289 | } |
| 290 | 290 | } |
@@ -292,7 +292,7 @@ discard block |
||
| 292 | 292 | } |
| 293 | 293 | |
| 294 | 294 | // if log condition is satisfied change the required log level to DEBUG |
| 295 | - if($this->logConditionSatisfied) { |
|
| 295 | + if ($this->logConditionSatisfied) { |
|
| 296 | 296 | $minLevel = Util::DEBUG; |
| 297 | 297 | } |
| 298 | 298 | |
@@ -324,9 +324,9 @@ discard block |
||
| 324 | 324 | 'File' => $exception->getFile(), |
| 325 | 325 | 'Line' => $exception->getLine(), |
| 326 | 326 | ); |
| 327 | - $data['Trace'] = preg_replace('!(' . implode('|', $this->methodsWithSensitiveParameters) . ')\(.*\)!', '$1(*** sensitive parameters replaced ***)', $data['Trace']); |
|
| 327 | + $data['Trace'] = preg_replace('!('.implode('|', $this->methodsWithSensitiveParameters).')\(.*\)!', '$1(*** sensitive parameters replaced ***)', $data['Trace']); |
|
| 328 | 328 | $msg = isset($context['message']) ? $context['message'] : 'Exception'; |
| 329 | - $msg .= ': ' . json_encode($data); |
|
| 329 | + $msg .= ': '.json_encode($data); |
|
| 330 | 330 | $this->log($level, $msg, $context); |
| 331 | 331 | } |
| 332 | 332 | |
@@ -32,71 +32,71 @@ |
||
| 32 | 32 | use Sabre\HTTP\Response; |
| 33 | 33 | |
| 34 | 34 | class ExceptionLoggerPlugin extends \Sabre\DAV\ServerPlugin { |
| 35 | - protected $nonFatalExceptions = [ |
|
| 36 | - 'Sabre\DAV\Exception\NotAuthenticated' => true, |
|
| 37 | - // If tokenauth can throw this exception (which is basically as |
|
| 38 | - // NotAuthenticated. So not fatal. |
|
| 39 | - 'OCA\DAV\Connector\Sabre\Exception\PasswordLoginForbidden' => true, |
|
| 40 | - // the sync client uses this to find out whether files exist, |
|
| 41 | - // so it is not always an error, log it as debug |
|
| 42 | - 'Sabre\DAV\Exception\NotFound' => true, |
|
| 43 | - // this one mostly happens when the same file is uploaded at |
|
| 44 | - // exactly the same time from two clients, only one client |
|
| 45 | - // wins, the second one gets "Precondition failed" |
|
| 46 | - 'Sabre\DAV\Exception\PreconditionFailed' => true, |
|
| 47 | - // forbidden can be expected when trying to upload to |
|
| 48 | - // read-only folders for example |
|
| 49 | - 'Sabre\DAV\Exception\Forbidden' => true, |
|
| 50 | - // Happens when an external storage or federated share is temporarily |
|
| 51 | - // not available |
|
| 52 | - 'Sabre\DAV\Exception\StorageNotAvailableException' => true, |
|
| 53 | - ]; |
|
| 35 | + protected $nonFatalExceptions = [ |
|
| 36 | + 'Sabre\DAV\Exception\NotAuthenticated' => true, |
|
| 37 | + // If tokenauth can throw this exception (which is basically as |
|
| 38 | + // NotAuthenticated. So not fatal. |
|
| 39 | + 'OCA\DAV\Connector\Sabre\Exception\PasswordLoginForbidden' => true, |
|
| 40 | + // the sync client uses this to find out whether files exist, |
|
| 41 | + // so it is not always an error, log it as debug |
|
| 42 | + 'Sabre\DAV\Exception\NotFound' => true, |
|
| 43 | + // this one mostly happens when the same file is uploaded at |
|
| 44 | + // exactly the same time from two clients, only one client |
|
| 45 | + // wins, the second one gets "Precondition failed" |
|
| 46 | + 'Sabre\DAV\Exception\PreconditionFailed' => true, |
|
| 47 | + // forbidden can be expected when trying to upload to |
|
| 48 | + // read-only folders for example |
|
| 49 | + 'Sabre\DAV\Exception\Forbidden' => true, |
|
| 50 | + // Happens when an external storage or federated share is temporarily |
|
| 51 | + // not available |
|
| 52 | + 'Sabre\DAV\Exception\StorageNotAvailableException' => true, |
|
| 53 | + ]; |
|
| 54 | 54 | |
| 55 | - /** @var string */ |
|
| 56 | - private $appName; |
|
| 55 | + /** @var string */ |
|
| 56 | + private $appName; |
|
| 57 | 57 | |
| 58 | - /** @var ILogger */ |
|
| 59 | - private $logger; |
|
| 58 | + /** @var ILogger */ |
|
| 59 | + private $logger; |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * @param string $loggerAppName app name to use when logging |
|
| 63 | - * @param ILogger $logger |
|
| 64 | - */ |
|
| 65 | - public function __construct($loggerAppName, $logger) { |
|
| 66 | - $this->appName = $loggerAppName; |
|
| 67 | - $this->logger = $logger; |
|
| 68 | - } |
|
| 61 | + /** |
|
| 62 | + * @param string $loggerAppName app name to use when logging |
|
| 63 | + * @param ILogger $logger |
|
| 64 | + */ |
|
| 65 | + public function __construct($loggerAppName, $logger) { |
|
| 66 | + $this->appName = $loggerAppName; |
|
| 67 | + $this->logger = $logger; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * This initializes the plugin. |
|
| 72 | - * |
|
| 73 | - * This function is called by \Sabre\DAV\Server, after |
|
| 74 | - * addPlugin is called. |
|
| 75 | - * |
|
| 76 | - * This method should set up the required event subscriptions. |
|
| 77 | - * |
|
| 78 | - * @param \Sabre\DAV\Server $server |
|
| 79 | - * @return void |
|
| 80 | - */ |
|
| 81 | - public function initialize(\Sabre\DAV\Server $server) { |
|
| 70 | + /** |
|
| 71 | + * This initializes the plugin. |
|
| 72 | + * |
|
| 73 | + * This function is called by \Sabre\DAV\Server, after |
|
| 74 | + * addPlugin is called. |
|
| 75 | + * |
|
| 76 | + * This method should set up the required event subscriptions. |
|
| 77 | + * |
|
| 78 | + * @param \Sabre\DAV\Server $server |
|
| 79 | + * @return void |
|
| 80 | + */ |
|
| 81 | + public function initialize(\Sabre\DAV\Server $server) { |
|
| 82 | 82 | |
| 83 | - $server->on('exception', array($this, 'logException'), 10); |
|
| 84 | - } |
|
| 83 | + $server->on('exception', array($this, 'logException'), 10); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Log exception |
|
| 88 | - * |
|
| 89 | - */ |
|
| 90 | - public function logException(\Exception $ex) { |
|
| 91 | - $exceptionClass = get_class($ex); |
|
| 92 | - $level = \OCP\Util::FATAL; |
|
| 93 | - if (isset($this->nonFatalExceptions[$exceptionClass])) { |
|
| 94 | - $level = \OCP\Util::DEBUG; |
|
| 95 | - } |
|
| 86 | + /** |
|
| 87 | + * Log exception |
|
| 88 | + * |
|
| 89 | + */ |
|
| 90 | + public function logException(\Exception $ex) { |
|
| 91 | + $exceptionClass = get_class($ex); |
|
| 92 | + $level = \OCP\Util::FATAL; |
|
| 93 | + if (isset($this->nonFatalExceptions[$exceptionClass])) { |
|
| 94 | + $level = \OCP\Util::DEBUG; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - $this->logger->logException($ex, [ |
|
| 98 | - 'app' => $this->appName, |
|
| 99 | - 'level' => $level, |
|
| 100 | - ]); |
|
| 101 | - } |
|
| 97 | + $this->logger->logException($ex, [ |
|
| 98 | + 'app' => $this->appName, |
|
| 99 | + 'level' => $level, |
|
| 100 | + ]); |
|
| 101 | + } |
|
| 102 | 102 | } |