Merge pull request #265 from Aoiujz/dev

添加日志记录,以及添加异常忽略支持
This commit is contained in:
Aoiujz
2016-02-01 23:47:08 +08:00
4 changed files with 27 additions and 10 deletions

View File

@@ -105,6 +105,9 @@ return [
// 异常页面的模板文件 // 异常页面的模板文件
'exception_tmpl' => THINK_PATH . 'tpl' . DS . 'think_exception.tpl', 'exception_tmpl' => THINK_PATH . 'tpl' . DS . 'think_exception.tpl',
// 异常处理忽略的错误类型支持PHP所有的错误级别常量多个级别可以用|运算法
// 参考http://php.net/manual/en/errorfunc.constants.php
'exception_ignore_type' => 0,
// 错误显示信息,非调试模式有效 // 错误显示信息,非调试模式有效
'error_message' => '页面错误!请稍后再试~', 'error_message' => '页面错误!请稍后再试~',
// 错误定向页面 // 错误定向页面

View File

@@ -11,7 +11,6 @@
namespace think; namespace think;
use think\Exception;
use think\exception\ErrorException; use think\exception\ErrorException;
class Error class Error
@@ -73,6 +72,9 @@ class Error
'message' => Config::get('show_error_msg') ? $exception->getMessage() : Config::get('error_message'), '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); self::output($exception, $data);
// 禁止往下传播已处理过的异常 // 禁止往下传播已处理过的异常
@@ -89,11 +91,16 @@ class Error
*/ */
public static function appError($errno, $errstr, $errfile, $errline) public static function appError($errno, $errstr, $errfile, $errline)
{ {
if ($errno & Config::get('exception_ignore_type')) {
// 忽略的异常记录到日志
Log::record("[{$errno}]{$errstr}[{$errfile}:{$errline}]", 'notic');
} else {
// 将错误信息托管至 think\exception\ErrorException // 将错误信息托管至 think\exception\ErrorException
throw new ErrorException($errno, $errstr, $errfile, $errline); throw new ErrorException($errno, $errstr, $errfile, $errline);
// 禁止往下传播已处理过的异常 // 禁止往下传播已处理过的异常
return true; return true;
} }
}
/** /**
* Shutdown Handler * Shutdown Handler
@@ -101,6 +108,9 @@ class Error
*/ */
public static function appShutdown() public static function appShutdown()
{ {
// 写入日志
Log::save();
if ($error = error_get_last()) { if ($error = error_get_last()) {
// 将错误信息托管至think\ErrorException // 将错误信息托管至think\ErrorException
$exception = new ErrorException( $exception = new ErrorException(

View File

@@ -45,7 +45,7 @@ class Exception extends \Exception
* @param string $label 数据分类,用于异常页面显示 * @param string $label 数据分类,用于异常页面显示
* @param Array $data 需要显示的数据,必须为关联数组 * @param Array $data 需要显示的数据,必须为关联数组
*/ */
final protected function setData($label, Array $data) final protected function setData($label, array $data)
{ {
$this->data[$label] = $data; $this->data[$label] = $data;
} }

View File

@@ -68,7 +68,11 @@ class File
foreach ($log as $line) { foreach ($log as $line) {
$info .= '[' . $line['type'] . '] ' . $line['msg'] . "\r\n"; $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);
} }
} }