Child pages
  • 라라벨 5 eloquent orm query log 남기기

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagephp
titleapp/Provides/AppServiceProvider.php
public function register()
{
    if ($this->app->environment() !== 'production') {
    	\Event::listen('Illuminate\Database\Events\QueryExecuted', function ($query) {
        	\Log::info([
            'sql' => $query->sql,
            'bindings' => $query->bindings,
            'time' => $query->time,
	        ]);
    	});
	}
}


일반 로그와 섞여서 보기가 불편하면 Log channel 기능을 사용하여 sql log file 을 분리

Code Block
titleconfig/logging.php
'channels' => [
  'sql' => [
    'driver' => 'daily',
    'path' => storage_path('logs/sql.log'),
    'level' => 'debug',
    'days' => 14,
  ],
Code Block
languagephp
titleapp/Provides/AppServiceProvider.php
public function register()
{
    if ($this->app->environment() !== 'production') {
    	\Event::listen('Illuminate\Database\Events\QueryExecuted', function ($query) {
        	\Log::channel('sql')->info([
            'sql' => $query->sql,
            'bindings' => $query->bindings,
            'time' => $query->time,
	        ]);
    	});
	}
}

방법 2. laravel-sql-logger 패키지 사용

...