@@ -30,90 +30,90 @@ |
||
| 30 | 30 | use OCP\ILogger; |
| 31 | 31 | |
| 32 | 32 | class ErrorHandler { |
| 33 | - /** @var ILogger */ |
|
| 34 | - private static $logger; |
|
| 33 | + /** @var ILogger */ |
|
| 34 | + private static $logger; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * remove password in URLs |
|
| 38 | - * @param string $msg |
|
| 39 | - * @return string |
|
| 40 | - */ |
|
| 41 | - protected static function removePassword($msg) { |
|
| 42 | - return preg_replace('/\/\/(.*):(.*)@/', '//xxx:xxx@', $msg); |
|
| 43 | - } |
|
| 36 | + /** |
|
| 37 | + * remove password in URLs |
|
| 38 | + * @param string $msg |
|
| 39 | + * @return string |
|
| 40 | + */ |
|
| 41 | + protected static function removePassword($msg) { |
|
| 42 | + return preg_replace('/\/\/(.*):(.*)@/', '//xxx:xxx@', $msg); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - public static function register($debug = false) { |
|
| 46 | - $handler = new ErrorHandler(); |
|
| 45 | + public static function register($debug = false) { |
|
| 46 | + $handler = new ErrorHandler(); |
|
| 47 | 47 | |
| 48 | - if ($debug) { |
|
| 49 | - set_error_handler([$handler, 'onAll'], E_ALL); |
|
| 50 | - if (\OC::$CLI) { |
|
| 51 | - set_exception_handler(['OC_Template', 'printExceptionErrorPage']); |
|
| 52 | - } |
|
| 53 | - } else { |
|
| 54 | - set_error_handler([$handler, 'onError']); |
|
| 55 | - } |
|
| 56 | - register_shutdown_function([$handler, 'onShutdown']); |
|
| 57 | - set_exception_handler([$handler, 'onException']); |
|
| 58 | - } |
|
| 48 | + if ($debug) { |
|
| 49 | + set_error_handler([$handler, 'onAll'], E_ALL); |
|
| 50 | + if (\OC::$CLI) { |
|
| 51 | + set_exception_handler(['OC_Template', 'printExceptionErrorPage']); |
|
| 52 | + } |
|
| 53 | + } else { |
|
| 54 | + set_error_handler([$handler, 'onError']); |
|
| 55 | + } |
|
| 56 | + register_shutdown_function([$handler, 'onShutdown']); |
|
| 57 | + set_exception_handler([$handler, 'onException']); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - public static function setLogger(ILogger $logger) { |
|
| 61 | - self::$logger = $logger; |
|
| 62 | - } |
|
| 60 | + public static function setLogger(ILogger $logger) { |
|
| 61 | + self::$logger = $logger; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - //Fatal errors handler |
|
| 65 | - public static function onShutdown() { |
|
| 66 | - $error = error_get_last(); |
|
| 67 | - if ($error && self::$logger) { |
|
| 68 | - //ob_end_clean(); |
|
| 69 | - $msg = $error['message'] . ' at ' . $error['file'] . '#' . $error['line']; |
|
| 70 | - self::$logger->critical(self::removePassword($msg), ['app' => 'PHP']); |
|
| 71 | - } |
|
| 72 | - } |
|
| 64 | + //Fatal errors handler |
|
| 65 | + public static function onShutdown() { |
|
| 66 | + $error = error_get_last(); |
|
| 67 | + if ($error && self::$logger) { |
|
| 68 | + //ob_end_clean(); |
|
| 69 | + $msg = $error['message'] . ' at ' . $error['file'] . '#' . $error['line']; |
|
| 70 | + self::$logger->critical(self::removePassword($msg), ['app' => 'PHP']); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Uncaught exception handler |
|
| 76 | - * |
|
| 77 | - * @param \Exception $exception |
|
| 78 | - */ |
|
| 79 | - public static function onException($exception) { |
|
| 80 | - $class = get_class($exception); |
|
| 81 | - $msg = $exception->getMessage(); |
|
| 82 | - $msg = "$class: $msg at " . $exception->getFile() . '#' . $exception->getLine(); |
|
| 83 | - self::$logger->critical(self::removePassword($msg), ['app' => 'PHP']); |
|
| 84 | - } |
|
| 74 | + /** |
|
| 75 | + * Uncaught exception handler |
|
| 76 | + * |
|
| 77 | + * @param \Exception $exception |
|
| 78 | + */ |
|
| 79 | + public static function onException($exception) { |
|
| 80 | + $class = get_class($exception); |
|
| 81 | + $msg = $exception->getMessage(); |
|
| 82 | + $msg = "$class: $msg at " . $exception->getFile() . '#' . $exception->getLine(); |
|
| 83 | + self::$logger->critical(self::removePassword($msg), ['app' => 'PHP']); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - //Recoverable errors handler |
|
| 87 | - public static function onError($number, $message, $file, $line) { |
|
| 88 | - if (error_reporting() === 0) { |
|
| 89 | - return; |
|
| 90 | - } |
|
| 91 | - $msg = $message . ' at ' . $file . '#' . $line; |
|
| 92 | - $e = new \Error(self::removePassword($msg)); |
|
| 93 | - self::$logger->logException($e, ['app' => 'PHP', 'level' => self::errnoToLogLevel($number)]); |
|
| 94 | - } |
|
| 86 | + //Recoverable errors handler |
|
| 87 | + public static function onError($number, $message, $file, $line) { |
|
| 88 | + if (error_reporting() === 0) { |
|
| 89 | + return; |
|
| 90 | + } |
|
| 91 | + $msg = $message . ' at ' . $file . '#' . $line; |
|
| 92 | + $e = new \Error(self::removePassword($msg)); |
|
| 93 | + self::$logger->logException($e, ['app' => 'PHP', 'level' => self::errnoToLogLevel($number)]); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - //Recoverable handler which catch all errors, warnings and notices |
|
| 97 | - public static function onAll($number, $message, $file, $line) { |
|
| 98 | - $msg = $message . ' at ' . $file . '#' . $line; |
|
| 99 | - $e = new \Error(self::removePassword($msg)); |
|
| 100 | - self::$logger->logException($e, ['app' => 'PHP', 'level' => self::errnoToLogLevel($number)]); |
|
| 101 | - } |
|
| 96 | + //Recoverable handler which catch all errors, warnings and notices |
|
| 97 | + public static function onAll($number, $message, $file, $line) { |
|
| 98 | + $msg = $message . ' at ' . $file . '#' . $line; |
|
| 99 | + $e = new \Error(self::removePassword($msg)); |
|
| 100 | + self::$logger->logException($e, ['app' => 'PHP', 'level' => self::errnoToLogLevel($number)]); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - public static function errnoToLogLevel(int $errno): int { |
|
| 104 | - switch ($errno) { |
|
| 105 | - case E_USER_WARNING: |
|
| 106 | - return ILogger::WARN; |
|
| 103 | + public static function errnoToLogLevel(int $errno): int { |
|
| 104 | + switch ($errno) { |
|
| 105 | + case E_USER_WARNING: |
|
| 106 | + return ILogger::WARN; |
|
| 107 | 107 | |
| 108 | - case E_USER_DEPRECATED: |
|
| 109 | - return ILogger::DEBUG; |
|
| 108 | + case E_USER_DEPRECATED: |
|
| 109 | + return ILogger::DEBUG; |
|
| 110 | 110 | |
| 111 | - case E_USER_NOTICE: |
|
| 112 | - return ILogger::INFO; |
|
| 111 | + case E_USER_NOTICE: |
|
| 112 | + return ILogger::INFO; |
|
| 113 | 113 | |
| 114 | - case E_USER_ERROR: |
|
| 115 | - default: |
|
| 116 | - return ILogger::ERROR; |
|
| 117 | - } |
|
| 118 | - } |
|
| 114 | + case E_USER_ERROR: |
|
| 115 | + default: |
|
| 116 | + return ILogger::ERROR; |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | 119 | } |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | $error = error_get_last(); |
| 67 | 67 | if ($error && self::$logger) { |
| 68 | 68 | //ob_end_clean(); |
| 69 | - $msg = $error['message'] . ' at ' . $error['file'] . '#' . $error['line']; |
|
| 69 | + $msg = $error['message'].' at '.$error['file'].'#'.$error['line']; |
|
| 70 | 70 | self::$logger->critical(self::removePassword($msg), ['app' => 'PHP']); |
| 71 | 71 | } |
| 72 | 72 | } |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | public static function onException($exception) { |
| 80 | 80 | $class = get_class($exception); |
| 81 | 81 | $msg = $exception->getMessage(); |
| 82 | - $msg = "$class: $msg at " . $exception->getFile() . '#' . $exception->getLine(); |
|
| 82 | + $msg = "$class: $msg at ".$exception->getFile().'#'.$exception->getLine(); |
|
| 83 | 83 | self::$logger->critical(self::removePassword($msg), ['app' => 'PHP']); |
| 84 | 84 | } |
| 85 | 85 | |
@@ -88,14 +88,14 @@ discard block |
||
| 88 | 88 | if (error_reporting() === 0) { |
| 89 | 89 | return; |
| 90 | 90 | } |
| 91 | - $msg = $message . ' at ' . $file . '#' . $line; |
|
| 91 | + $msg = $message.' at '.$file.'#'.$line; |
|
| 92 | 92 | $e = new \Error(self::removePassword($msg)); |
| 93 | 93 | self::$logger->logException($e, ['app' => 'PHP', 'level' => self::errnoToLogLevel($number)]); |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | //Recoverable handler which catch all errors, warnings and notices |
| 97 | 97 | public static function onAll($number, $message, $file, $line) { |
| 98 | - $msg = $message . ' at ' . $file . '#' . $line; |
|
| 98 | + $msg = $message.' at '.$file.'#'.$line; |
|
| 99 | 99 | $e = new \Error(self::removePassword($msg)); |
| 100 | 100 | self::$logger->logException($e, ['app' => 'PHP', 'level' => self::errnoToLogLevel($number)]); |
| 101 | 101 | } |