Cách log thông tin lỗi trogn exceptions php

Phần này mình sẽ giới thiệu với mọi người về report lỗi, và xử lý lỗi trong Laravel. Trong Laravel tất cả các error, exception đều chạy qua class App\Exceptions\Handler để làm nhiệm vụ ghi log rồi hiển thị đến người dùng.

1. Cấu hình.

Cấu hình debug trong config/app.php là tham số để quyết định việc có hiển thị thông tin lỗi đến người dùng hay không. Mặc định tham số này được xác định qua APP_DEBUG trong file .env, nếu là

/   @inheritDoc  / protected function context() {     return array_merge(parent::context(), [         'url' => URL::current(),     ]); }

0 thì khi gặp lỗi Laravel sẽ hiển thị các thông tin của lỗi đó lên trên màn hình cho user và ngược lại nếu là false thì Laravel chỉ hiển thị status code là 500 trên màn hình cho user.

Cách log thông tin lỗi trogn exceptions php

Demo màn hình report lỗi khi ở chế độ debug mode là true

Lưu ý: Các bạn chỉ nên bật mode debug khi ở trên môi trường local, hoặc dev thôi. Còn trên production tuyệt đối không bao giờ được để là

/   @inheritDoc  / protected function context() {     return array_merge(parent::context(), [         'url' => URL::current(),     ]); }

0. Vì như thế sẽ làm lộ ra các thông tin cấu hình trên ứng dụng của bạn.

2. Exception Handler.

Như mình đã nói ở trên, tất cả các exception đều được xử lý qua

/   @inheritDoc  / protected function context() {     return array_merge(parent::context(), [         'url' => URL::current(),     ]); }

2. Trong class này các bạn có thể khai báo các custom logic trong phương thức register, phương thức này sẽ rất hữu dụng nếu như bạn muốn sử dụng thêm một ứng dụng error tracking của bên thứ 3.

VD:

use App\Exceptions\InvalidOrderException; /   Register the exception handling callbacks for the application.     @return void  / public function register() {     $this->reportable(function (InvalidOrderException $e) {         //     }); }

Ngoài ra bạn cũng có thể tùy chỉnh context của exception bằng cách overwrite lại phương thức

/   @inheritDoc  / protected function context() {     return array_merge(parent::context(), [         'url' => URL::current(),     ]); }

3 trong class cha.

VD: Mình sẽ thêm URL gây ra lỗi vào context.

/   @inheritDoc  / protected function context() {     return array_merge(parent::context(), [         'url' => URL::current(),     ]); }

Nếu như bạn muốn loại bỏ một exception nào đó, không muốn report exception nữa. Bạn có thể cấu hình trong

/   @inheritDoc  / protected function context() {     return array_merge(parent::context(), [         'url' => URL::current(),     ]); }

4 property.

VD: Loại bỏ

/   @inheritDoc  / protected function context() {     return array_merge(parent::context(), [         'url' => URL::current(),     ]); }

5 ra khỏi danh sách report.

/   A list of the exception types that are not reported.     @var array  / protected $dontReport = [     ProductNotFoundException::class, ];

Mặc đinh, Laravel sẽ tự động convert các Exception về dạng HTTP response. Tuy nhiên, nếu như bạn cần custom response cho một exception nào đó bạn có thể sử dụng method

/   @inheritDoc  / protected function context() {     return array_merge(parent::context(), [         'url' => URL::current(),     ]); }

6.

VD: Mình sẽ render ra view

/   @inheritDoc  / protected function context() {     return array_merge(parent::context(), [         'url' => URL::current(),     ]); }

7 khi gặp

/   @inheritDoc  / protected function context() {     return array_merge(parent::context(), [         'url' => URL::current(),     ]); }

5.

/   Register the exception handling callbacks for the application.     @return void  / public function register() {     $this->renderable(function(ProductNotFoundException $exception) {         return response()->view('errors.product_not_found', [], 500);     }); }

Ngoài ra bạn muốn thay đổi các view error trong Laravel, bạn có thể tạo ra các view trong thư mục

/   @inheritDoc  / protected function context() {     return array_merge(parent::context(), [         'url' => URL::current(),     ]); }

9 với tên là các mã lỗi tương ứng. Ví dụ:

/   A list of the exception types that are not reported.     @var array  / protected $dontReport = [     ProductNotFoundException::class, ];

0

Hoặc bạn cũng có thể publish hết các error view trong core của Laravel ra thư mục resources/views/errors để chỉnh sửa cho tiện. Bạn có thể sử dụng câu lệnh.

php artisan vendor:publish --tag=laravel-errors

Trong trường hợp, bạn muốn render ra một view error nào đó mà không muốn raise exception trong đoạn code đó. Bạn có thể sử dụng hàm

/   A list of the exception types that are not reported.     @var array  / protected $dontReport = [     ProductNotFoundException::class, ];

1 với cú pháp.

abort($code, $message,$headers)

Trong đó:

  • /   A list of the exception types that are not reported.     @var array  / protected $dontReport = [     ProductNotFoundException::class, ]; 2 là mã http code mà bạn muốn reder ra.
  • /   A list of the exception types that are not reported.     @var array  / protected $dontReport = [     ProductNotFoundException::class, ]; 3 là message mà bạn muốn reder ra kèm theo http code. Trường này có thể bỏ trống.
  • /   A list of the exception types that are not reported.     @var array  / protected $dontReport = [     ProductNotFoundException::class, ]; 4 là các thông số header bạn muốn đẩy vào http response. Trường này có thể bỏ trống.

3. Lời kết.

Phần này mình chỉ giới thiệu với mọi người những gì liên quan đến exception handle thôi, còn các thứ khác như logging mình sẽ giới thiệu với mọi người trong một bài khác.