diff --git a/convention.php b/convention.php index fa1409cb..60bbeeed 100644 --- a/convention.php +++ b/convention.php @@ -105,6 +105,9 @@ return [ // 异常页面的模板文件 'exception_tmpl' => THINK_PATH . 'tpl' . DS . 'think_exception.tpl', + // 异常处理忽略的错误类型,支持PHP所有的错误级别常量,多个级别可以用|运算法 + // 参考:http://php.net/manual/en/errorfunc.constants.php + 'exception_ignore_type' => 0, // 错误显示信息,非调试模式有效 'error_message' => '页面错误!请稍后再试~', // 错误定向页面 diff --git a/library/think/Error.php b/library/think/Error.php index 9370f4d7..6b6218c8 100644 --- a/library/think/Error.php +++ b/library/think/Error.php @@ -11,7 +11,6 @@ namespace think; -use think\Exception; use think\exception\ErrorException; class Error @@ -73,6 +72,9 @@ class Error 'message' => Config::get('show_error_msg') ? $exception->getMessage() : Config::get('error_message'), ]; } + + // 记录异常日志 + Log::write("[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]", 'error'); // 输出错误信息 self::output($exception, $data); // 禁止往下传播已处理过的异常 @@ -89,10 +91,15 @@ class Error */ public static function appError($errno, $errstr, $errfile, $errline) { - // 将错误信息托管至 think\exception\ErrorException - throw new ErrorException($errno, $errstr, $errfile, $errline); - // 禁止往下传播已处理过的异常 - return true; + if ($errno & Config::get('exception_ignore_type')) { + // 忽略的异常记录到日志 + Log::record("[{$errno}]{$errstr}[{$errfile}:{$errline}]", 'notic'); + } else { + // 将错误信息托管至 think\exception\ErrorException + throw new ErrorException($errno, $errstr, $errfile, $errline); + // 禁止往下传播已处理过的异常 + return true; + } } /** @@ -101,6 +108,9 @@ class Error */ public static function appShutdown() { + // 写入日志 + Log::save(); + if ($error = error_get_last()) { // 将错误信息托管至think\ErrorException $exception = new ErrorException( diff --git a/library/think/Exception.php b/library/think/Exception.php index ff6e4726..abf32018 100644 --- a/library/think/Exception.php +++ b/library/think/Exception.php @@ -15,7 +15,7 @@ namespace think; * ThinkPHP核心异常类 * 所有系统异常必须继承该类 */ -class Exception extends \Exception +class Exception extends \Exception { /** * 系统异常后发送给客户端的HTTP Status @@ -32,7 +32,7 @@ class Exception extends \Exception /** * 设置异常额外的Debug数据 * 数据将会显示为下面的格式 - * + * * Exception Data * -------------------------------------------------- * Label 1 @@ -41,11 +41,11 @@ class Exception extends \Exception * Label 2 * key1 value1 * key2 value2 - * + * * @param string $label 数据分类,用于异常页面显示 * @param Array $data 需要显示的数据,必须为关联数组 */ - final protected function setData($label, Array $data) + final protected function setData($label, array $data) { $this->data[$label] = $data; } diff --git a/library/think/log/driver/File.php b/library/think/log/driver/File.php index 6cd01c2d..5f0c8e2a 100644 --- a/library/think/log/driver/File.php +++ b/library/think/log/driver/File.php @@ -68,7 +68,11 @@ class File foreach ($log as $line) { $info .= '[' . $line['type'] . '] ' . $line['msg'] . "\r\n"; } - error_log("[{$now}] {$_SERVER['SERVER_ADDR']} {$_SERVER['REMOTE_ADDR']} {$_SERVER['REQUEST_URI']}\r\n{$info}\r\n", 3, $destination); + + $server = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '0.0.0.0'; + $remote = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0'; + $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; + error_log("[{$now}] {$server} {$remote} {$uri}\r\n{$info}\r\n", 3, $destination); } }