user1633272
user1633272

Reputation: 2309

How to log db queries in Lumen5.7?

I upgrade Lumen from 5.4 to 5.7, and I want to be able to log DB queries for debugging.

Here's the conf/source code. I have to use 'LumenDB' alias because of naming conflict of a third-party library.

I expect the query could be logged, but I can not see them in 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);
        });
    }

The query is executed inside a Service method which is called by a Command scheduled by cron.

public function getAllStatsToday()
{
    $today = new \DateTime();
    $today->setTime(0, 0, 0);
    $productUsageStats = ProductUsageStat::make()
        ->where('updated_at', '>', $today)
        ->get();
    return $productUsageStats;
}

Upvotes: 1

Views: 930

Answers (1)

Sven Hakvoort
Sven Hakvoort

Reputation: 3621

You have not registered the AppServiceProvider in your bootstrap/app.php. Because of this the boot method in your AppServiceProvider is never registered and thus the logging is never executed.

You should change app.php to the following:

$app->register(App\Providers\EventServiceProvider::class);

$app->register(App\Providers\AppServiceProvider::class);

\LumenDB::connection()->enableQueryLog();

Upvotes: 1

Related Questions