一、改变日志记录位置
//public的index.php中改变日志保存路径
<?php
// [ 应用入口文件 ]
// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
//自定义日志位置 覆盖原来日志位置
define('LOG_PATH', __DIR__ . '/../log/');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';
效果
二、在全局异常处理中加入日志系统
//controller 控制器 banner 抛出异常
<?php
namespace app\api\controller\v1;
use app\api\validate\IDMustBePostiveInt;
use app\api\model\Banner as BannerModel;
use app\lib\exception\BannerMissException;
use think\Exception;
class Banner
{
public function getBanner($id){
(new IDMustBePostiveInt()) -> goCheck();
$banner = BannerModel::getBannerById($id);
if(!$banner){
//抛出异常 方式一
throw new Exception('内部错误');
//抛出异常 方式二 自定义异常
// throw new BannerMissException();
}
return $banner;
}
}
//调用打印方法 打印日志
<?php
namespace app\lib\exception;
use think\Exception;
use think\exception\Handle;
use think\Log;
use think\Request;
class ExceptionHandler extends Handle
{
private $code;
private $msg;
private $errorCode;
//客户要返回客户端的URL路径
public function render(\Exception $e)
{
if($e instanceof BaseException){
//如果是自定义的异常
$this -> code = $e -> code;
$this -> msg = $e -> msg;
$this -> errorCode = $e -> errorCode;
}else{
//如果不是自定义的异常
$this -> code = 500;
$this -> msg = '服务器内部错误,不想告诉你!';
$this -> errorCode = 999;
//打印日志
$this -> recordErrorLog($e);
}
$request = Request::instance();
$result = [
'msg' => $this -> msg,
'error_code' => $this -> errorCode,
'request_url' => $request -> url()
];
return json($result, $this -> code);
}
private function recordErrorLog(Exception $e){
Log::init([
'type' => 'File',
'path' => LOG_PATH,
'level' => ['error']
]);
Log::record($e -> getMessage(), 'error');
}
}
三、根据是否开启调试 判断是否返回具体错误信息页面 config('app_debug')
// 根据是否开启调试 判断是否返回具体错误信息页面 config('app_debug')
<?php
namespace app\lib\exception;
use think\Config;
use think\Exception;
use think\exception\Handle;
use think\Log;
use think\Request;
class ExceptionHandler extends Handle
{
private $code;
private $msg;
private $errorCode;
//客户要返回客户端的URL路径
public function render(\Exception $e)
{
if($e instanceof BaseException){
//如果是自定义的异常
$this -> code = $e -> code;
$this -> msg = $e -> msg;
$this -> errorCode = $e -> errorCode;
}else{
//Config::get('app_debug');
if(config('app_debug')){
//return default error page
return parent::render($e);
}else{
//如果不是自定义的异常
$this -> code = 500;
$this -> msg = '服务器内部错误,不想告诉你!';
$this -> errorCode = 999;
//打印日志
$this -> recordErrorLog($e);
}
}
$request = Request::instance();
$result = [
'msg' => $this -> msg,
'error_code' => $this -> errorCode,
'request_url' => $request -> url()
];
return json($result, $this -> code);
}
private function recordErrorLog(Exception $e){
Log::init([
'type' => 'File',
'path' => LOG_PATH,
'level' => ['error']
]);
Log::record($e -> getMessage(), 'error');
}
}
四、 兼容验证层异常抛出
目录
//创建参数错误异常 ParameterException 并继承BaseException
<?php
namespace app\lib\exception;
class ParameterException extends BaseException
{
public $code = 400;
public $msg = '参数错误';
public $errorCode = 10000;
}
//BaseException 异常基类
<?php
namespace app\lib\exception;
use think\Exception;
class BaseException extends Exception
{
//HTTP 状态码 404,200
public $code = 400;
//错误具体信息
public $msg = '参数错误';
//自定义的错误码
public $errorCode = 10000;
}
//控制器
<?php
namespace app\api\controller\v1;
use app\api\validate\IDMustBePostiveInt;
use app\api\model\Banner as BannerModel;
use app\lib\exception\BannerMissException;
use think\Exception;
class Banner
{
public function getBanner($id){
(new IDMustBePostiveInt()) -> goCheck();
//$banner = BannerModel::getBannerById($id);
//return $banner;
}
}
//验证层基类 BaseValidate
<?php
namespace app\api\validate;
use app\lib\exception\ParameterException;
use think\Exception;
use think\Request;
use think\Validate;
class BaseValidate extends Validate
{
public function goCheck(){
//获取http传入的参数
//对这些参数做校验
$request = Request::instance();
$params = $request -> param();
$result = $this -> check($params);
if(!$result){
$e = new ParameterException();
$e -> msg = $this -> getError();
$e -> errorCode = 10002;
throw $e;
// $error = $this->getError();
// throw new Exception($error);
}else{
return true;
}
}
}
//IDMustBePostiveInt 参数必须是正整数验证
<?php
namespace app\api\validate;
class IDMustBePostiveInt extends BaseValidate
{
protected $rule = [
'id' => 'require|isPostiveInteger'
];
//field是字段名
protected function isPostiveInteger($value, $rule = '', $data = '', $field = ''){
if(is_numeric($value) && is_int($value + 0) && ($value + 0) > 0){
return true;
}else{
return $field.'必须是正整数';
}
}
}
五、使用构造函数改造 异常参数传递
//参数传递 使用构造函数 BaseValidate验证器
<?php
namespace app\api\validate;
use app\lib\exception\ParameterException;
use think\Exception;
use think\Request;
use think\Validate;
class BaseValidate extends Validate
{
public function goCheck(){
//获取http传入的参数
//对这些参数做校验
$request = Request::instance();
$params = $request -> param();
$result = $this -> check($params);
if(!$result){
$e = new ParameterException([
'msg' => '',
//'code' => 400,
//'error' => 10002
]);
throw $e;
}else{
return true;
}
}
}
//构造函数接收参数 参数可选
<?php
namespace app\lib\exception;
use think\Exception;
use Throwable;
class BaseException extends Exception
{
//HTTP 状态码 404,200
public $code = 400;
//错误具体信息
public $msg = '参数错误';
//自定义的错误码
public $errorCode = 10000;
public function __construct($params = [])
{
if(!is_array($params)){
return;
//throw new Exception('参数必须是数组');
}
if(array_key_exists('code', $params)){
$this -> code = $params['code'];
}
if(array_key_exists('msg', $params)){
$this -> msg = $params['msg'];
}
if(array_key_exists('errorCode', $params)){
$this -> errorCode = $params['errorCode'];
}
}
}
六、验证器兼容多个参数错误
//check前使用 batch()方法
<?php
namespace app\api\validate;
use app\lib\exception\ParameterException;
use think\Exception;
use think\Request;
use think\Validate;
class BaseValidate extends Validate
{
public function goCheck(){
//获取http传入的参数
//对这些参数做校验
$request = Request::instance();
$params = $request -> param();
$result = $this -> batch() -> check($params);
if(!$result){
$e = new ParameterException([
'msg' => $this -> error,
// 'code' => 400,
// 'error' => 10002
]);
throw $e;
}else{
return true;
}
}
}
效果
来源:CSDN
作者:aaron9185
链接:https://blog.csdn.net/aaron9185/article/details/104317427