如何从雄辩的多个连接中记录每个查询(外部Laravel)
我在应用程序,一个SQLServ和另一个MySQL中使用多个数据库连接。我想依次从两个服务器上调试每个查询。因此,请使用Manager :: getQueryLog()
我需要使用event :: Listen
。我使用Slimframework,带有PHP-DI。
index.php
// Create container & database
$containerBuilder = new DI\ContainerBuilder(App\Lib\Container::class);
$containerBuilder->useAnnotations(false);
$containerBuilder->addDefinitions(__DIR__ . '/../config/settings.php');
$container = $containerBuilder->build();
$app = \DI\Bridge\Slim\Bridge::create($container);
// Register database
$capsule = new \Illuminate\Database\Capsule\Manager();
foreach ($container->get('database') as $con => $config) {
$capsule->addConnection($config, $con);
}
$capsule->setEventDispatcher(new \Illuminate\Events\Dispatcher());
// Throw error A facade root has not been set. pretty sure it
// was because i use it outside laravel
$capsule->setAsGlobal();
$capsule->bootEloquent();
$container->set('connection', $capsule);
// Listen
DB::listen(function($query) {
Log::info(
$query->sql,
$query->bindings,
$query->time
);
});
app \ lib \ container :: class
namespace App\Lib;
use DI\Container as DIContainer;
class Container extends DIContainer
{
public function __get($key)
{
if ($this->has($key)) {
return $this->get($key);
}
}
}
如何从多个连接中记录每个查询,但是按顺序排序,如下所示。
select * from tableInMySQL limit 0,10;
select TOP 10 * from [tableInMSSQL];
update tableInMySQL set field='value';
编辑
就像我之前说过的,我使用 slimframework,带有php-di 。
因此,我整体上都不使用 laravel 。 (不使用服务提供商)
db ::听听
投掷错误,$ capsule-> getConnection('con_name') - > getEventDisPatcher()
return> return
I use multiple database connections in my app, one SQLServ, and another MySQL. I want to debug every query from both servers sequentially. therefore rather using Manager::getQueryLog()
i need to use Event::listen
. I use SlimFramework, with PHP-DI.
index.php
// Create container & database
$containerBuilder = new DI\ContainerBuilder(App\Lib\Container::class);
$containerBuilder->useAnnotations(false);
$containerBuilder->addDefinitions(__DIR__ . '/../config/settings.php');
$container = $containerBuilder->build();
$app = \DI\Bridge\Slim\Bridge::create($container);
// Register database
$capsule = new \Illuminate\Database\Capsule\Manager();
foreach ($container->get('database') as $con => $config) {
$capsule->addConnection($config, $con);
}
$capsule->setEventDispatcher(new \Illuminate\Events\Dispatcher());
// Throw error A facade root has not been set. pretty sure it
// was because i use it outside laravel
$capsule->setAsGlobal();
$capsule->bootEloquent();
$container->set('connection', $capsule);
// Listen
DB::listen(function($query) {
Log::info(
$query->sql,
$query->bindings,
$query->time
);
});
App\Lib\Container::class
namespace App\Lib;
use DI\Container as DIContainer;
class Container extends DIContainer
{
public function __get($key)
{
if ($this->has($key)) {
return $this->get($key);
}
}
}
How to log every query from multiple connections, but in sequential order, something like the following.
select * from tableInMySQL limit 0,10;
select TOP 10 * from [tableInMSSQL];
update tableInMySQL set field='value';
EDIT
like I said earlier, I use SlimFramework, with PHP-DI.
So, I'm not use LARAVEL as a whole. (not using service provider)
the DB::listen
throw error, $capsule->getConnection('con_name')->getEventDispatcher()
return null
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要在顺序顺序中记录多个DB连接的查询,我们需要
EnableQueryLog
首先,对单独的Logger进行了配置。To log queries of multiple db connections in sequential order, we'll need to
enableQueryLog
first, have separate Logger configured.对于在这里登陆的任何人,我们都需要在每个连接上附加听众
sett.php
index.php
for anyone who's landing here, we need to attach listener on every connection
setting.php
index.php
您应该在
boot()
app/app/provister/appserviceprovider.php 的功能中编写db ::收听事件。you should write DB::listen event in
boot()
function ofapp/Providers/AppServiceProvider.php
请将其添加到提供商/AppServiceProvider.php文件中,然后在Laravel日志文件中检查它们
Please add this to the Providers/AppServiceProvider.php file and check them in the laravel log file
我将那里的代码放入appserviceprovider.php上的boot()方法中,
因此,日志只能为开发环境编写。
I put there code into boot() method on AppServiceProvider.php
So, the log will be write only for development environment.