From 1fc0783ebf3116520f75c1384bd41cc178c4e75f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BA=A6=E5=BD=93=E8=8B=97=E5=84=BF?= Date: Mon, 1 Feb 2016 21:08:08 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=EF=BC=8C=E4=BB=A5=E5=8F=8A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=BF=BD=E7=95=A5=E6=94=AF=E6=8C=81=20?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=BF=BD=E7=95=A5=E6=94=AF=E6=8C=81=E5=BF=BD?= =?UTF-8?q?=E7=95=A5=E6=8C=87=E5=AE=9A=E7=9A=84=E5=BC=82=E5=B8=B8=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=EF=BC=8C=E8=A2=AB=E5=BF=BD=E7=95=A5=E7=9A=84=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E4=BB=85=E4=BB=85=E8=AE=B0=E5=BD=95=E5=88=B0=E6=97=A5?= =?UTF-8?q?=E5=BF=97=EF=BC=8C=E4=B8=8D=E4=BC=9A=E4=B8=AD=E6=96=AD=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E6=89=A7=E8=A1=8C=20=E5=BF=BD=E7=95=A5=E7=9A=84?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E7=B1=BB=E5=9E=8B=E6=94=AF=E6=8C=81PHP?= =?UTF-8?q?=E7=9A=84=E6=89=80=E6=9C=89=E9=94=99=E8=AF=AF=E7=BA=A7=E5=88=AB?= =?UTF-8?q?=EF=BC=8C=E5=A4=9A=E4=B8=AA=E7=BA=A7=E5=88=AB=E6=94=AF=E6=8C=81?= =?UTF-8?q?=20|=20=E8=BF=90=E7=AE=97=E7=AC=A6=20=E5=8F=82=E8=80=83?= =?UTF-8?q?=EF=BC=9Ahttp://php.net/manual/en/errorfunc.constants.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- convention.php | 3 +++ library/think/Error.php | 20 +++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) 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..331722e4 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("[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]", '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( From 338532b024cdc48fb75e5b223487bd3edc37b0b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BA=A6=E5=BD=93=E8=8B=97=E5=84=BF?= Date: Mon, 1 Feb 2016 21:14:34 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=9A=84=E5=8F=98=E9=87=8F=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Error.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/Error.php b/library/think/Error.php index 331722e4..6b6218c8 100644 --- a/library/think/Error.php +++ b/library/think/Error.php @@ -93,7 +93,7 @@ class Error { if ($errno & Config::get('exception_ignore_type')) { // 忽略的异常记录到日志 - Log::record("[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]", 'notic'); + Log::record("[{$errno}]{$errstr}[{$errfile}:{$errline}]", 'notic'); } else { // 将错误信息托管至 think\exception\ErrorException throw new ErrorException($errno, $errstr, $errfile, $errline); From 73c931089de25675bb11c70b8edd7aab340507ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BA=A6=E5=BD=93=E8=8B=97=E5=84=BF?= Date: Mon, 1 Feb 2016 23:40:11 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8DNotice=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Exception.php | 8 ++++---- library/think/log/driver/File.php | 5 ++++- 2 files changed, 8 insertions(+), 5 deletions(-) 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..89eb000a 100644 --- a/library/think/log/driver/File.php +++ b/library/think/log/driver/File.php @@ -68,7 +68,10 @@ 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'; + error_log("[{$now}] {$server} {$remote} {$_SERVER['REQUEST_URI']}\r\n{$info}\r\n", 3, $destination); } } From f0353910a53948b59af0bfe44ee7c75fdf106797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BA=A6=E5=BD=93=E8=8B=97=E5=84=BF?= Date: Mon, 1 Feb 2016 23:44:35 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8DNotice=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/log/driver/File.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/think/log/driver/File.php b/library/think/log/driver/File.php index 89eb000a..5f0c8e2a 100644 --- a/library/think/log/driver/File.php +++ b/library/think/log/driver/File.php @@ -71,7 +71,8 @@ class File $server = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '0.0.0.0'; $remote = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0'; - error_log("[{$now}] {$server} {$remote} {$_SERVER['REQUEST_URI']}\r\n{$info}\r\n", 3, $destination); + $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; + error_log("[{$now}] {$server} {$remote} {$uri}\r\n{$info}\r\n", 3, $destination); } }