|
16 | 16 | class PHPErrors |
17 | 17 | { |
18 | 18 | public $logger; |
| 19 | + public static $reportLevel = E_ALL; |
19 | 20 |
|
20 | 21 | public static function enable($errorReportLevel = E_ALL, $devMode = true) |
21 | 22 | { |
22 | 23 | if ($errorReportLevel != null) { |
23 | 24 | error_reporting($errorReportLevel); |
| 25 | + self::$reportLevel = $errorReportLevel; |
24 | 26 | } else { |
25 | | - error_reporting(-1); |
| 27 | + error_reporting(E_ALL); |
26 | 28 | } |
27 | 29 |
|
28 | 30 | if ($devMode) { |
@@ -70,30 +72,44 @@ public function handleFatalError() |
70 | 72 |
|
71 | 73 | if (!empty($error)) { |
72 | 74 | $type = $error['type']; |
73 | | - $message = $this->formatMessage($error['message'], $error['file'], $error['line']); |
74 | 75 |
|
75 | | - return $this->log($type, $message); |
| 76 | + if ($type & self::$reportLevel) { |
| 77 | + $message = $this->formatMessage($error['message'], $error['file'], $error['line']); |
| 78 | + |
| 79 | + return $this->log($type, $message); |
| 80 | + } |
76 | 81 | } |
77 | 82 | } |
78 | 83 |
|
79 | 84 | public function handleError($type, $message, $file, $line) |
80 | 85 | { |
81 | | - $message = $this->formatMessage($message, $file, $line); |
| 86 | + if ($type & self::$reportLevel) { |
| 87 | + $message = $this->formatMessage($message, $file, $line); |
82 | 88 |
|
83 | | - return $this->log($type, $message); |
| 89 | + return $this->log($type, $message); |
| 90 | + } |
84 | 91 | } |
85 | 92 |
|
86 | 93 | public function handleException(\Throwable $exception) |
87 | 94 | { |
88 | | - if ($exception instanceof \Error || $exception instanceof \ErrorException) { |
89 | | - $type = $exception instanceof \Error ? $exception->getCode() : $exception->getSeverity(); |
90 | | - $message = $this->formatMessage( |
91 | | - $exception->getMessage(), |
92 | | - $exception->getFile(), |
93 | | - $exception->getLine() |
94 | | - ); |
| 95 | + //Error code 需要转换成 Exception 的错误级别 |
| 96 | + //这里把所有的 Error 都转换成 E_ERROR 级别异常, 用来做捕获 |
| 97 | + if ($exception instanceof \Error) { |
| 98 | + $exception = new \ErrorException($exception->getMessage(), 0, E_ERROR, $exception->getFile(), $exception->getLine()); |
| 99 | + } |
95 | 100 |
|
96 | | - return $this->log($type, $message); |
| 101 | + if ($exception instanceof \ErrorException) { |
| 102 | + $type = $exception->getSeverity(); |
| 103 | + |
| 104 | + if ($type & self::$reportLevel) { |
| 105 | + $message = $this->formatMessage( |
| 106 | + $exception->getMessage(), |
| 107 | + $exception->getFile(), |
| 108 | + $exception->getLine() |
| 109 | + ); |
| 110 | + |
| 111 | + return $this->log($type, $message); |
| 112 | + } |
97 | 113 | } |
98 | 114 | } |
99 | 115 |
|
|
0 commit comments