smurrlawa/laravel-query-logger

A Laravel package to log all database queries to a configurable log channel or handler.

v0.1.0 2025-06-14 11:11 UTC

This package is auto-updated.

Last update: 2025-06-14 16:19:58 UTC


README

A simple Laravel package to log all database queries to a configurable log channel. Supports conditional logging by environment and slow query reporting.

πŸ“¦ Installation

composer require smurrlawa/laravel-query-logger

βš™οΈ Configuration

Publish the config file

php artisan vendor:publish --tag=query-logger-config

This will publish the configuration to config/query-logger.php.

Available Options

return [
    'enabled' => env('QUERY_LOGGER_ENABLED', true),

    'channel' => env('QUERY_LOGGER_CHANNEL', 'daily'),

    'slow_queries_enabled' => env('QUERY_LOGGER_SLOW_QUERIES_ENABLED', true),

    'slow_queries_threshold' => env('QUERY_LOGGER_SLOW_QUERIES_THRESHOLD', 1000),

    'environments' => ['local', 'staging'],
];

βœ… Option Descriptions

Option Description
enabled Enable or disable query logging completely.
channel The log channel to use (config/logging.php or PSR logger class).
slow_queries_enabled Whether to log slow queries separately.
slow_queries_threshold Time in milliseconds to consider a query as slow.
environments List of environments where query logging should be active.

πŸ§ͺ Example Usage

To log all queries in local environment and warn about slow queries:

APP_ENV=local

QUERY_LOGGER_ENABLED=true
QUERY_LOGGER_CHANNEL=daily

QUERY_LOGGER_SLOW_QUERIES_ENABLED=true
QUERY_LOGGER_SLOW_QUERIES_THRESHOLD=500

πŸš€ Features

πŸ“ Logs all database queries (SQL, bindings, execution time)

🐒 Logs slow queries exceeding a customizable threshold

βš™οΈ Fully configurable via config file and environment variables

🌍 Supports environment-specific logging (e.g., local, staging)

πŸ“‘ Supports custom log channels and PSR-3 loggers

βœ… Compatibility

Tested with Laravel 11 and Laravel 12.

πŸ“ License

MIT Β© Smurrlawa