This commit is contained in:
80
app/Listener/DbQueryExecutedListener.php
Normal file
80
app/Listener/DbQueryExecutedListener.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace App\Listener;
|
||||
|
||||
use Hyperf\Collection\Arr;
|
||||
use Hyperf\Database\Events\QueryExecuted;
|
||||
use Hyperf\Event\Annotation\Listener;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
use Hyperf\Logger\LoggerFactory;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
#[Listener]
|
||||
class DbQueryExecutedListener implements ListenerInterface
|
||||
{
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
private LoggerInterface $logger;
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->logger = $container->get(LoggerFactory::class)->get('sql', 'sql');
|
||||
}
|
||||
|
||||
public function listen(): array
|
||||
{
|
||||
return [
|
||||
QueryExecuted::class,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param object $event
|
||||
*/
|
||||
public function process(object $event): void
|
||||
{
|
||||
/**
|
||||
* 处理QueryExecuted事件,记录查询的SQL和执行时间。
|
||||
*
|
||||
* @param QueryExecuted $event 该事件对象包含执行的SQL语句、绑定参数和执行时间
|
||||
*/
|
||||
if ($event instanceof QueryExecuted) {
|
||||
$sql = $event->sql; // 获取执行的SQL语句
|
||||
|
||||
// 检查绑定参数是否为关联数组,如果不是,将 "?" 替换为实际的参数值
|
||||
if (!Arr::isAssoc($event->bindings)) {
|
||||
$position = 0; // 初始化绑定参数的搜索位置
|
||||
foreach ($event->bindings as $value) { // 遍历绑定参数
|
||||
$position = strpos($sql, '?', $position); // 查找下一个 "?" 位置
|
||||
if ($position === false) { // 如果找不到 "?",则退出循环
|
||||
break;
|
||||
}
|
||||
$value = "'$value'"; // 将参数值包裹在单引号中
|
||||
$sql = substr_replace($sql, $value, $position, 1); // 替换 "?" 为实际参数值
|
||||
$position += strlen($value); // 更新搜索位置
|
||||
}
|
||||
}
|
||||
|
||||
// 使用logger记录SQL语句和执行时间
|
||||
$this->logger->info(sprintf('[%s] %s', $event->time, $sql));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user