我将Lumen从5.4升级到5.7,并且希望能够记录数据库查询以进行调试。
这是conf /源代码。由于第三方库的命名冲突,我必须使用“ LumenDB ”别名。
我希望可以记录该查询,但在lumen.log中看不到它们。
<?php
namespace App;
use Illuminate\Support\Facades\Facade;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
class MyApplication extends \Laravel\Lumen\Application {
public function withFacades($aliases = true, $userAliases = [])
{
Facade::setFacadeApplication($this);
if (! static::$aliasesRegistered) {
static::$aliasesRegistered = true;
class_alias('Illuminate\Support\Facades\Auth', 'Auth');
class_alias('Illuminate\Support\Facades\Cache', 'Cache');
class_alias('Illuminate\Support\Facades\DB', 'LumenDB');
class_alias('Illuminate\Support\Facades\Event', 'Event');
class_alias('Illuminate\Support\Facades\Gate', 'Gate');
class_alias('Illuminate\Support\Facades\Log', 'Log');
class_alias('Illuminate\Support\Facades\Queue', 'Queue');
class_alias('Illuminate\Support\Facades\Schema', 'Schema');
class_alias('Illuminate\Support\Facades\Validator', 'Validator');
}
}
protected function registerLogBindings()
{
$this->singleton('Psr\Log\LoggerInterface', function () {
return new Logger('lumen', $this->getMonologHandler());
});
}
protected function getMonologHandler()
{
$maxFiles = 7;
$rotatingLogHandler = new RotatingFileHandler(storage_path('logs/lumen.log'), $maxFiles);
$rotatingLogHandler->setFormatter(new LineFormatter(null, null, true, true));
$handlers = [];
$handlers[] = $rotatingLogHandler;
return $handlers;
}
}
$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);
\LumenDB::connection()->enableQueryLog();
use Illuminate\Support\Facades\DB;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
DB::listen(function ($query) {
// $query->sql
// $query->bindings
// $query->time
Log::info("-------");
Log::info($query->sql);
});
}
查询是在Service方法内部执行的,该方法由cron调度的Command调用。
public function getAllStatsToday()
{
$today = new \DateTime();
$today->setTime(0, 0, 0);
$productUsageStats = ProductUsageStat::make()
->where('updated_at', '>', $today)
->get();
return $productUsageStats;
}
答案 0 :(得分:1)
您尚未在AppServiceProvider
中注册bootstrap/app.php
。因此,您的boot
中的AppServiceProvider
方法不会被注册,因此永远不会执行日志记录。
您应将app.php
更改为以下内容:
$app->register(App\Providers\EventServiceProvider::class);
$app->register(App\Providers\AppServiceProvider::class);
\LumenDB::connection()->enableQueryLog();