PHPackages                             elminson/database-query-logger - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. elminson/database-query-logger

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

elminson/database-query-logger
==============================

A powerful PHP package for logging SQL queries from Laravel applications, supporting both Eloquent and Query Builder instances, with flexible output options.

1.1.3(1y ago)2552[3 PRs](https://github.com/elminson/database-query-logger/pulls)MITPHPPHP ^8.1CI passing

Since Jan 30Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/elminson/database-query-logger)[ Packagist](https://packagist.org/packages/elminson/database-query-logger)[ GitHub Sponsors](https://github.com/elminson)[ RSS](/packages/elminson-database-query-logger/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (23)Used By (0)

Database Query Logger
=====================

[](#database-query-logger)

[![Latest Version on Packagist](https://camo.githubusercontent.com/1431636970daa5a8d005e99b461551d0e6ab2d2f76dd8be1686900e2107a1493/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f656c6d696e736f6e2f64617461626173652d71756572792d6c6f676765722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/elminson/database-query-logger)[![Tests](https://camo.githubusercontent.com/a1af8bf214ea9f8de9ef026a5215559101f563f0666af8d6449f73d0cf801716/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f656c6d696e736f6e2f64622d6c6f676765722f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/elminson/db-logger/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/57921eed9294b6a94e53326270ae5552dc325863577fa06d8c92fd735dee19ae/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f656c6d696e736f6e2f64617461626173652d71756572792d6c6f676765722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/elminson/database-query-logger)

A powerful PHP package for logging SQL queries from Laravel applications, supporting both Eloquent and Query Builder instances, with flexible output options.

Installation
------------

[](#installation)

You can install the package via Composer:

```
composer require elminson/database-query-logger
```

Features
--------

[](#features)

- Log SQL queries from Eloquent or Query Builder instances
- Log SQL queries to a file or the console
- Log SQL queries with parameter bindings
- Support for PDO statements
- Configurable logging options
- Timestamp-based log entries
- Automatic directory creation for log files
- Support for different parameter types (string, integer, boolean, null)
- JSON log formatting
- Log rotation (daily/weekly, max files)

Configuration
-------------

[](#configuration)

### Environment Variables

[](#environment-variables)

Add these variables to your `.env` file:

```
DB_LOGGER_ENABLED=true
DB_LOGGER_CONSOLE_OUTPUT=true
DB_LOGGER_FILE_LOGGING=true
DB_LOGGER_FILE_PATH=storage/logs/queries.log
DB_LOGGER_LOG_FORMAT=text # or json
DB_LOGGER_ROTATION_ENABLED=false
DB_LOGGER_ROTATION_PERIOD=daily # or weekly
DB_LOGGER_ROTATION_MAX_FILES=7
```

### Configuration Options

[](#configuration-options)

The logger can be configured using environment variables (as shown above) or by passing an array to its constructor. If you publish the configuration file (`config/db-logger.php`), you can manage these settings there.

Here are the available options:

- `enabled` (boolean): Enable or disable the logger completely. Default: `false`.
    - Env: `DB_LOGGER_ENABLED`
- `console_output` (boolean): Enable or disable console output. Default: `false`.
    - Env: `DB_LOGGER_CONSOLE_OUTPUT`
- `file_logging` (boolean): Enable or disable file logging. Default: `false`.
    - Env: `DB_LOGGER_FILE_LOGGING`
- `log_file` (string): Path to the log file. Default: `storage_path('logs/database-queries.log')`.
    - Env: `DB_LOGGER_FILE_PATH`
- `log_format` (string): Log format. Possible values: `text`, `json`. Default: `text`.
    - Env: `DB_LOGGER_LOG_FORMAT`
- `log_rotation_enabled` (boolean): Enable or disable log rotation. Default: `false`.
    - Env: `DB_LOGGER_ROTATION_ENABLED`
- `log_rotation_period` (string): Log rotation period. Possible values: `daily`, `weekly`. Default: `daily`.
    - Env: `DB_LOGGER_ROTATION_PERIOD`
- `log_rotation_max_files` (integer): Maximum number of rotated log files to keep. Default: `7`.
    - Env: `DB_LOGGER_ROTATION_MAX_FILES`

### Publishing the Configuration File

[](#publishing-the-configuration-file)

If you want to customize the logger configuration, you can publish the config file to your Laravel project using the following Artisan command:

```
php artisan vendor:publish --provider="Elminson\\DbLogger\\DatabaseQueryLoggerServiceProvider" --tag=config
```

This will copy the configuration file to `config/db-logger.php` in your Laravel application, where you can adjust the settings as needed.

### Service Provider

[](#service-provider)

Register the service provider in `config/app.php`:

```
'providers' => [
    // ...
    App\Providers\DatabaseQueryLoggerServiceProvider::class,
],
```

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

```
use Elminson\DbLogger\DatabaseQueryLogger;
use Illuminate\Database\Capsule\Manager as DB;

// Initialize the logger
$logger = new DatabaseQueryLogger([
    'enabled' => true,
    'console_output' => true,
    'file_logging' => true,
    'log_file' => storage_path('logs/queries.log'),
    'log_format' => 'text',   // or 'json'
    'log_rotation_enabled' => false, // or true
    'log_rotation_period' => 'daily', // or 'weekly'
    'log_rotation_max_files' => 7
]);

// Log a query
$query = DB::table('users')->where('email', 'example@example.com');
$logger->logQuery($query);
```

### PDO Statement Logging

[](#pdo-statement-logging)

```
use PDO;
use PDOStatement;

$pdo = new PDO('sqlite::memory:');
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email');
$stmt->bindParam(':email', 'example@example.com');

$logger->logQuery($stmt, ['example@example.com']);
```

### Raw Query Logging

[](#raw-query-logging)

```
$sql = 'SELECT * FROM users WHERE email = ?';
$bindings = ['example@example.com'];
$logger->logQuery($sql, $bindings, $connection);
```

### Configuration Methods

[](#configuration-methods)

```
// Enable/disable logging
$logger->enable(true);

// Enable/disable console output
$logger->enableConsoleOutput(true);

// Set log file path
$logger->setLogFile(storage_path('logs/queries.log'));
```

Testing
-------

[](#testing)

Run the tests with:

```
composer test
```

Run tests with coverage:

```
composer test-coverage
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Credits
-------

[](#credits)

- [Elminson De Oleo Baez](https://github.com/elminson)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

Log All Queries in Laravel
--------------------------

[](#log-all-queries-in-laravel)

To log every SQL query executed by your Laravel application, add the following to your `app/Providers/AppServiceProvider.php`:

```
use Illuminate\Support\Facades\DB;
use Elminson\DbLogger\DatabaseQueryLogger;

public function boot()
{
    $logger = new DatabaseQueryLogger(config('db-logger'));

    DB::listen(function ($query) use ($logger) {
        $logger->logQuery(
            $query->sql,
            $query->bindings,
            $query->connection
        );
    });
}
```

Make sure your `config/db-logger.php` and `.env` are set up as described above. This will ensure all queries are logged according to your configuration.

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance72

Regular maintenance activity

Popularity13

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 81.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~9 days

Recently: every ~0 days

Total

13

Last Release

366d ago

Major Versions

0.0.7 → 1.0.02025-05-17

PHP version history (2 changes)0.0.1PHP ^8.3

0.0.2PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/e399869d29ffb004ec4652fee201a7a794169cfe1218fa4771a520419bfa02b0?d=identicon)[elminson](/maintainers/elminson)

---

Top Contributors

[![elminson](https://avatars.githubusercontent.com/u/2476286?v=4)](https://github.com/elminson "elminson (60 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (5 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (5 commits)")[![ivanmercedes](https://avatars.githubusercontent.com/u/53806989?v=4)](https://github.com/ivanmercedes "ivanmercedes (2 commits)")[![google-labs-jules[bot]](https://avatars.githubusercontent.com/in/842251?v=4)](https://github.com/google-labs-jules[bot] "google-labs-jules[bot] (1 commits)")[![masterfermin02](https://avatars.githubusercontent.com/u/4625540?v=4)](https://github.com/masterfermin02 "masterfermin02 (1 commits)")

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/elminson-database-query-logger/health.svg)

```
[![Health](https://phpackages.com/badges/elminson-database-query-logger/health.svg)](https://phpackages.com/packages/elminson-database-query-logger)
```

###  Alternatives

[spatie/laravel-activitylog

A very simple activity logger to monitor the users of your website or application

5.8k45.4M309](/packages/spatie-laravel-activitylog)[spatie/laravel-health

Monitor the health of a Laravel application

86910.0M83](/packages/spatie-laravel-health)[yadahan/laravel-authentication-log

Laravel Authentication Log provides authentication logger and notification for Laravel.

416632.8k5](/packages/yadahan-laravel-authentication-log)[directorytree/metrics

Record metrics in your Laravel application

26027.8k](/packages/directorytree-metrics)[masterro/laravel-mail-viewer

Easily view in browser outgoing emails.

6392.1k](/packages/masterro-laravel-mail-viewer)[label84/laravel-auth-log

Log user authentication actions in Laravel.

3654.0k](/packages/label84-laravel-auth-log)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
