renov's avatar
Level 4

Log sqlite query in Telescope / Debugbar

I use SQLite (in file, not in memory) for some of my models.

I would like to log the underlying queries in my debugbar and/or Telescope as it happens for the underlying queries I have for models queried from MySQL. For now, I see the loaded models both in my debugbar and Telescope but the queries are not logged.

I don't find any configuration in the Debugbar or Telescope to log SQLite queries.

Any idea how I can do it?

0 likes
2 replies
LaryAI's avatar
Level 58

To log SQLite queries in Laravel Telescope or Laravel Debugbar, you should ensure that your application is set up correctly to log queries regardless of the database driver being used. Here's how you can ensure that your SQLite queries are logged:

  1. Enable Query Logging in Laravel: Laravel allows you to listen for query events, which you can use to log queries. You can place the following code in a service provider, such as AppServiceProvider, within the boot method:

    use Illuminate\Support\Facades\DB;
    use Illuminate\Support\Facades\Log;
    
    public function boot()
    {
        if (env('APP_DEBUG')) {
            DB::listen(function ($query) {
                Log::info($query->sql, $query->bindings, $query->time);
            });
        }
    }
    

    This will log all queries to the default Laravel log file. However, Laravel Debugbar and Telescope should automatically pick up these queries without this step, as they hook into the same underlying mechanism that Laravel uses for query logging.

  2. Configure Laravel Debugbar: If you're using Laravel Debugbar, make sure it's installed and enabled in your config/debugbar.php configuration file. You should have something like this:

    'enabled' => env('DEBUGBAR_ENABLED', true),
    

    Also, ensure that the collectors array has the queries enabled:

    'collectors' => [
        // ...
        'queries' => true,
        // ...
    ],
    
  3. Configure Laravel Telescope: For Laravel Telescope, ensure that you have the Watchers\QueryWatcher enabled in your config/telescope.php configuration file:

    'watchers' => [
        // ...
        \Laravel\Telescope\Watchers\QueryWatcher::class => [
            'enabled' => env('TELESCOPE_QUERY_WATCHER', true),
            'ignore_packages' => true,
            'slow' => 100,
        ],
        // ...
    ],
    
  4. Check Your Environment: Both Laravel Debugbar and Telescope are typically used in local development environments. Make sure that your .env file has the APP_DEBUG variable set to true:

    APP_DEBUG=true
    
  5. Clear Configuration Cache: If you've made changes to your configuration files, make sure to clear the configuration cache with the following command:

    php artisan config:clear
    
  6. Check Database Configuration: Finally, ensure that your SQLite database connection is configured correctly in your config/database.php file. It should look something like this:

    'connections' => [
        // ...
        'sqlite' => [
            'driver' => 'sqlite',
            'url' => env('DATABASE_URL'),
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
        ],
        // ...
    ],
    

If after following these steps, you still do not see SQLite queries in Laravel Debugbar or Telescope, there might be an issue specific to your application setup or a bug in the package. In such cases, it's a good idea to check the GitHub issues for the respective packages or seek further assistance from the community.

renov's avatar
Level 4

Reading the automatic AI reply, I think it may be related to the database configuration (point #6).

Indeed I use dynamic database configuration with the Sushi package

    protected static function setSqliteConnection($database)
    {
        $config = [
            'driver' => 'sqlite',
            'database' => $database,
        ];

        static::$sushiConnection = app(ConnectionFactory::class)->make($config);

        app('config')->set('database.connections.'.static::class, $config);
    }

cf. https://github.com/calebporzio/sushi/blob/b362dd4617d2b65e5661b0bcbebc043859d18336/src/Sushi.php#L113 so I think it may explain why Telescope/Debubar cannot resolve the connections and then log the queries.

I have no idea how I can instruct them to use those dynamics connections. Any clue around here?

Please or to participate in this conversation.