簡介
這一章也是屬於文檔寫得比較混亂的一章,所以我決定重新組織一下內容結構;
配置
debug配置
我們都知道,開發環境應該把debug打開,生產環境應該把debug關閉;
這個設置在config/app.php文件里,找到這一項:
'debug' => env('APP_DEBUG', false),
優先配置.env的值,后面是默認值。 true把debug打開,false關閉;
log模式配置
同樣在config/app.php文件里,找到:
'log' => 'single',
這里一個有4個可用選項值:"single", "daily", "syslog", "errorlog", 他們分別是:
- 單文件日志;
- 每日一文件日志;
- 系統日志;
- 錯誤日志;
處理錯誤對象及報錯信息
看app\Exceptions\handler.php這個文件,這是處理所有錯誤信息的關鍵文件:
namespace App\Exceptions;use Exception;use Symfony\Component\HttpKernel\Exception\HttpException;use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;class Handler extends ExceptionHandler{protected $dontReport = [HttpException::class,];public function report(Exception $e){return parent::report($e);}public function render($request, Exception $e){return parent::render($request, $e);}}
一個屬性,2個方法;
$dontReport
先來看$dontReport屬性,意思是不要log的錯誤信息,這里面填了HttpException::class, 也就是說,404這種錯誤, 如果你在這里設置HttpException,laravel會觸發,但不會記錄的。
你還可以添加更多不報告的錯誤類;
report()
這個只是把錯誤對象傳遞一下,傳到基類ExceptionHandler中去,你也可以在這里面把錯誤類型發布到一些外部服務。
不明白什么意思就跳過,這里不常用;
render()
render()是渲染錯誤視圖的地方,我們主要的自定義邏輯寫在這里;
我們最常見的錯誤類型可能就是:
ModelNotFoundException

於是我們可以這樣寫:
public function render($request, Exception $e){switch($e){case ($e instanceof ModelNotFoundException):return response()->view('errors.404', [], 404);breakdefault:return parent::render($request, $e);}}
這樣遇到這個ModelNotFoundException錯誤類的時候,就會觸發'errors.404'這個視圖;
自定義錯誤類
首先,在app\Exceptions\里建立一個自定義類:
namespace App\Exceptions;class CustomException extends \Exception{}
然后,在handler.php 的handler()方法內,判斷並處理CustomException,顯示相應的視圖;
public function render($request, Exception $e){switch($e){case ($e instanceof ModelNotFoundException):return response()->view('errors.404', [], 404);breakcase ($e instanceof CustomException):return response()->view('errors.custom',['exception'=>$e]);breakdefault:return parent::render($request, $e);}}
觸發自定義錯誤類
try{App\User::find(-1);}catch(){throw new App\Exceptions\CustomException;}
錯誤視圖常用的一些顯示錯誤的方法
@extends('template.master')@section('content')<h1>@{{ $exception->getStatusCode() }}</h1><p>@if(!empty($exception->getMessage()))@{{ $exception->getMessage() }}@else@{{ \Symfony\Component\HttpFoundation\Response::$statusTexts[$exception->getStatusCode()] }}@endif</p>@endsection
HTTP錯誤
自定義http錯誤頁面
發生http錯誤時,系統會先在resources/views/errors/這個文件夾內找與http錯誤代碼同名的blade文件,比如400錯誤就會找resources/views/errors/404.blade.php, 所以自定義http錯誤顯示頁面,只要在resources/views/errors/按照http錯誤代碼號建立對應的blade文件就行了。
拋出http錯誤代碼
一般,服務器上會設置什么時候拋出http錯誤代碼,但你也可以手動拋出,方法是:
abort(401); abort(500);abort(403)
填上對應的錯誤代碼號即可,這個方法在request的整個生命周期內都可用;
Logging
Laravel的日志功能基於強大的 Monolog 類庫,默認情況下是每天生成一個日志文件的模式,日子文件存儲在storage/logs;
插入log信息
你可以在代碼中向log日志插入信息,一共有這些方法可以使用:
Log::emergency($error);Log::alert($error);Log::critical($error);Log::error($error);Log::warning($error);Log::notice($error);Log::info($error);Log::debug($error);
代碼示例:
public function showProfile($id){Log::info('Showing user profile for user: '.$id);return view('user.profile', ['user' => User::findOrFail($id)]);}
上下文信息
Log::info('User failed to login.', ['id' => $user->id]);
你可以像這樣插入第二個參數,數組內的信息將會被格式化,在log顯示出來;
獲取Monolog對象
$monolog = Log::getMonolog();
像這樣獲取Monolog對象,Monolog對象中有很多方法可以操作log信息。
