DEV Community

Cover image for Output Laravel SQL Query log
Briano Trần
Briano Trần

Posted on

1

Output Laravel SQL Query log

You’ve likely encountered many query issues while developing software with Laravel. You might be wondering why your features are slow or how many queries your function is making to the database. You may not know if you’re experiencing N+1 problems or how to optimize your queries. This package is here to support you with those challenges.

Install
To install via Composer, run:

composer require bri4n0/sql-query-log
Enter fullscreen mode Exit fullscreen mode

For version <= 11: Add to section providers of config/app.php:

// config/app.php
'providers' => [
    ...
    Bri4n0\SqlQueryLog\DataBaseQueryServiceProvider::class,
];
Enter fullscreen mode Exit fullscreen mode

For version >= 11: Add to section providers of bootstrap/providers.php:

// bootstrap/providers.php
return [
    ...
    Bri4n0\SqlQueryLog\DataBaseQueryServiceProvider::class,
];
Enter fullscreen mode Exit fullscreen mode

Set enable SQL query log, Add to .env:

ENABLE_SQL_LOG=true
Enter fullscreen mode Exit fullscreen mode

Others
Custom logging channel:
open file config/logging.php add channel to section channels:

  'channels' => [
    ...,
     'sql-query-log' => [
            'driver'               => 'daily',
            'path'                 => storage_path('logs/queries.log'),
            'days'                 => env('LOG_DAILY_DAYS', 14),
            'replace_placeholders' => true,
        ],
    ]
Enter fullscreen mode Exit fullscreen mode

enable using sql query log channel:

SQL_LOG_CHANNEL=sql-query-log
Enter fullscreen mode Exit fullscreen mode

Custom logging channel level:

SQL_LOG_LEVEL=debug
Enter fullscreen mode Exit fullscreen mode

show more: https://github.com/bri4n0/sql-query-log

Top comments (0)

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay