TP5的日志如果沒有特别設定的話,隻要出現錯誤就會記錄在日志檔案中。這種是沒有做必要的,因為這樣的話,LOG檔案會特别大,而且有好多都是沒有用的資訊。下面我們對LOG日志進行改造隻記錄我們需要的資訊。
還記得上一章中,異常封裝中。看記錄日志出現的位置,我們隻記錄未知錯誤,而一些使用者引起的一些參數錯誤 ,我們在這裡不記錄。
class ExceptionHandler extends Handle
{
private $code;
private $msg;
private $errorCode;
public function render(Exception $e)
{
if($e instanceof BaseException){ //是否為BaseException繼承類
$this->code =$e ->code;
$this->msg =$e ->msg;
$this->errorCode =$e ->errorCode;
}else{
if(config("app_debug")==true){ //如是開啟調試,就走原來的方法
return parent::render($e);
}else{ //如是關閉調試,是未知錯誤,我們隻需要統一回複
$this->code = 500;
$this->msg = 'sorry,we make a mistake. (^o^)Y';
$this->errorCode = 999;
$this->recodeErrorLog($e); //記錄到日志中,這個下一節會講
}
}
$request = Request::instance(); //參數執行個體
$result=[
"msg"=> $this->msg,
"errorCode" =>$this->errorCode,
"require_url"=>$request->url() //取出通路時的URL
];
return json($result,$this->code);
}
我們先找開config.php這個檔案,找到日志存放的位置,我們順着找到,可能會出現在REANTIME那個目錄下面。

我們需要改路徑的話,隻需要在入口檔案中定義一個常量。如圖:
我們已經更改了日志存放的路徑,那麼我們要在我們需要的時候開啟日志怎麼辦,我們先要在
config.php這個檔案把type關掉
那麼我們現在是不能記錄日志的了,因為我們把日志關了,現在我們要在
ExceptionHandler開啟日志,
當我們要記錄日志時,我們隻要調用這個方法就行