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