原创

PHP-Laravel 框架 监听SQL打印

1.先在项目根目录使用命令生成文件:QueryListener

php artisan make:listener QueryListener --event=Illuminate\\Database\\Events\\QueryExecuted

2.再到EventServiceProvider文件中编辑:$listen

protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
'Illuminate\Database\Events\QueryExecuted' => [
'App\Listeners\QueryListener'
]
];

3.编辑生成的文件QueryListener

use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Facades\Log;

class QueryListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Handle the event.
*
* @param QueryExecuted $event
* @return void
*/
public function handle (QueryExecuted $event)
{
if (env('APP_ENV', 'production') == 'local') {
$sql = str_replace("?", "'%s'", $event->sql);
$log = vsprintf($sql, $event->bindings);
Log::info($log);
}
}
}


正文到此结束
该篇文章的评论功能已被站长关闭
本文目录