Debug 模式(調試/開發模式)
配置文件:
config/app.php
- 開發時, 建議打開這個模式,既設置 APP_DEBUG = true
- 上線時, 建議關閉調試模式,既設置 APP_DEBUG = false
http異常及自定義異常頁面
-
常見的 http錯誤碼
- 404 頁面未找到
- 500 服務器內部錯誤
-
自定義出現錯誤是的模板
- 在控制器中向客戶端拋出一個異常,使用
abort
方法
- 在控制器中向客戶端拋出一個異常,使用
public function customErrorPage(){
$test = null;
if ($test == null) {
abort('500');
}
}
- 自定義模板
在
resources/views
目錄下新建一個errors
目錄, 在這個目錄下自定義錯誤頁面模板,定義模板的名稱必須和abort
方法中的參數一致
日志
- 日志的配置文件是:
/config/app.php
- laravel 提供了
single
daily
syslog
error
這幾種日志模式, 默認single
-
debug
info
noteice
warning
error
critical
和alert
七個錯誤級別 - 生產的日志文件存放在
/storage/logs
這個目錄中 - 日志有什么用?
日志記錄着程序運行過程中所有的錯誤, 如果運行過程中出現了錯誤,而你又不知道是什么原因,錯誤還無法重現, 此時,你就可以去查看錯誤
- 如何記錄日志
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class TestController extends Controller{
public function log(){
// 錯誤有七個級別
Log::info('test log');
Log::warning('test log222');
Log::error('這是一個是error級別的日志信息', [
'error message' => 'error info.....',
'error code' => 1234,
]);
}
}