今回はLogファザードを使ったデバッグ方法で、JavaScriptからfetch()でリクエストされた値をを確認する。

ユッケです。
設定
最初に.envファイルでAPP_DEBUGがtrueであることを確認。
デフォルトでtrueになっているはず。
デバッグしたい箇所にLogファサードを記述
Logファザードの使い方は超簡単。
Illuminate\Support\Facades\Logを上に記載した後に、
確認したい変数や配列などを「Log::debug()」の引数に入れてログファイルを確認するだけ。
今回は下記でログを出力する。
my_project/src/my_project/app/Http/Requests/CustomerSearchRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Support\Facades\Log; // ←追記
class CustomerSearchRequest extends FormRequest
{
// 下記追記
protected function prepareForValidation()
{
Log::debug('---------------------------------');
Log::debug($this);
Log::debug('---------------------------------');
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'data' => ['required'],
'data.startDate' => 'required',
'data.endDate' => 'required',
];
}
public function messages()
{
return [
'data.required' => 'リクエストが不正です。(data:必須)',
'data.startDate.required' => 'リクエストが不正です。(startDate:必須)',
'data.endDate.required' => 'リクエストが不正です。(endDate:必須)',
];
}
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Support\Facades\Log; // ←追記
class CustomerSearchRequest extends FormRequest
{
// 下記追記
protected function prepareForValidation()
{
Log::debug('---------------------------------');
Log::debug($this);
Log::debug('---------------------------------');
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'data' => ['required'],
'data.startDate' => 'required',
'data.endDate' => 'required',
];
}
public function messages()
{
return [
'data.required' => 'リクエストが不正です。(data:必須)',
'data.startDate.required' => 'リクエストが不正です。(startDate:必須)',
'data.endDate.required' => 'リクエストが不正です。(endDate:必須)',
];
}
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Http\Exceptions\HttpResponseException; use Illuminate\Contracts\Validation\Validator; use Illuminate\Support\Facades\Log; // ←追記 class CustomerSearchRequest extends FormRequest { // 下記追記 protected function prepareForValidation() { Log::debug('---------------------------------'); Log::debug($this); Log::debug('---------------------------------'); } /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'data' => ['required'], 'data.startDate' => 'required', 'data.endDate' => 'required', ]; } public function messages() { return [ 'data.required' => 'リクエストが不正です。(data:必須)', 'data.startDate.required' => 'リクエストが不正です。(startDate:必須)', 'data.endDate.required' => 'リクエストが不正です。(endDate:必須)', ]; }
ログの出力先は標準ログ「storage/logs/laravel.log」に記述される。
Git Bashでtailコマンドでリクエストの値を確認
$ cd /c/Data/WebSite/my_project/src/my_app/storage
$ tail -f logs/laravel.log
// ここで、リクエスト送信。すると下記みたいにログが出力される。
[2023-02-03 08:23:55] local.DEBUG: ---------------------------------
[2023-02-03 08:23:55] local.DEBUG: array (
'sessionId' => 'hogehoge',
'data' =>
array (
'startDate' => '2022-10-01',
'endDate' => '2022-10-31',
),
)
[2023-02-03 08:23:55] local.DEBUG: ---------------------------------
$ cd /c/Data/WebSite/my_project/src/my_app/storage
$ tail -f logs/laravel.log
// ここで、リクエスト送信。すると下記みたいにログが出力される。
[2023-02-03 08:23:55] local.DEBUG: ---------------------------------
[2023-02-03 08:23:55] local.DEBUG: array (
'sessionId' => 'hogehoge',
'data' =>
array (
'startDate' => '2022-10-01',
'endDate' => '2022-10-31',
),
)
[2023-02-03 08:23:55] local.DEBUG: ---------------------------------
$ cd /c/Data/WebSite/my_project/src/my_app/storage $ tail -f logs/laravel.log // ここで、リクエスト送信。すると下記みたいにログが出力される。 [2023-02-03 08:23:55] local.DEBUG: --------------------------------- [2023-02-03 08:23:55] local.DEBUG: array ( 'sessionId' => 'hogehoge', 'data' => array ( 'startDate' => '2022-10-01', 'endDate' => '2022-10-31', ), ) [2023-02-03 08:23:55] local.DEBUG: ---------------------------------
ログレベルについて
Log::emergency("emergency");
Log::alert("alert");
Log::critical("critical");
Log::error("error");
Log::warning("warning");
Log::notice("notice");
Log::info("info");
Log::debug("debug");
Log::emergency("emergency");
Log::alert("alert");
Log::critical("critical");
Log::error("error");
Log::warning("warning");
Log::notice("notice");
Log::info("info");
Log::debug("debug");
Log::emergency("emergency"); Log::alert("alert"); Log::critical("critical"); Log::error("error"); Log::warning("warning"); Log::notice("notice"); Log::info("info"); Log::debug("debug");