【发布时间】:2019-04-14 10:48:59
【问题描述】:
我将 Lumen 从 5.4 升级到 5.7,我希望能够记录 DB 查询以进行调试。
这是 conf/源代码。由于第三方库的命名冲突,我必须使用“LumenDB”别名。
我希望可以记录查询,但我在 lumen.log 中看不到它们。
MyApplication.php
<?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;
}
}
bootstrap/app.php
$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);
\LumenDB::connection()->enableQueryLog();
app/Providers/AppServiceProvider.php
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 调度的命令调用。
public function getAllStatsToday()
{
$today = new \DateTime();
$today->setTime(0, 0, 0);
$productUsageStats = ProductUsageStat::make()
->where('updated_at', '>', $today)
->get();
return $productUsageStats;
}
【问题讨论】:
-
你能提供你的config/logging.php吗?您是否重新启动了应用程序?
-
@SvenHakvoort 我没有 config/logging.php,日志是在 MyApplication 中配置的(我刚刚编辑了帖子),我可以看到除了 db 日志之外的其他日志。 “重新启动应用程序”是什么意思?我只是将源文件更新到服务器上。
-
似乎“函数 ($query)”由于某种原因从未被调用过。
-
我将 logging.cpp 从 vendor/laravel 复制到配置,它没有帮助。
标签: php laravel laravel-5 lumen