APP_ENVがlocalの時にのみ、
Storage/logs/laravel-日付.logに出力する簡易版
.env
DB_QUERY_LOG=true
反映
$ php artisan config:clear
Providers/AppServiceProvider.php
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\DB; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { if (env('DB_QUERY_LOG', false)) { \DB::listen( function ($query) { $sql = $query->sql; for ($i = 0; $i < count($query->bindings); $i++) { $sql = preg_replace("/\?/", $query->bindings[$i], $sql, 1); } \Log::channel('query')->debug( "SQL", ["time" => sprintf("%.2f ms", $query->time), "sql" => $sql] ); } ); } }
'channels' => [ 'stack' => [ 'driver' => 'stack', - 'channels' => ['daily'], + 'channels' => ['daily', 'query'], 'ignore_exceptions' => false, ], (略) 'query' => [ 'driver' => 'daily', 'path' => storage_path('logs/query.log'), 'level' => 'debug', 'days' => 90 ],