PHPackages                             onlime/laravel-sql-reporter - 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. [Database &amp; ORM](/categories/database)
4. /
5. onlime/laravel-sql-reporter

ActiveLibrary[Database &amp; ORM](/categories/database)

onlime/laravel-sql-reporter
===========================

Log SQL queries in Laravel framework

v1.3.6(2mo ago)11890↓50%1MITPHPPHP ^8.3CI failing

Since Jun 2Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/onlime/laravel-sql-reporter)[ Packagist](https://packagist.org/packages/onlime/laravel-sql-reporter)[ RSS](/packages/onlime-laravel-sql-reporter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (14)Versions (19)Used By (0)

Laravel SQL Reporter
====================

[](#laravel-sql-reporter)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d5271ab36720bcfd6020412f79026fdc20fbf16bebd17dd95a1aa29c34e88287/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f6e6c696d652f6c61726176656c2d73716c2d7265706f727465722e737667)](https://packagist.org/packages/onlime/laravel-sql-reporter)[![Packagist Downloads](https://camo.githubusercontent.com/97e74004fa4cfe55f4e713080b6081432e7d628518f1d16c632cee7e18c63328/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f6e6c696d652f6c61726176656c2d73716c2d7265706f727465722e737667)](https://packagist.org/packages/onlime/laravel-sql-reporter)[![PHP from Packagist](https://camo.githubusercontent.com/dcc5b15593b2e35ba12c41b79eea3cc18eeb83134ee82a21e0d78b7467776281/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6f6e6c696d652f6c61726176656c2d73716c2d7265706f727465722e737667)](https://packagist.org/packages/onlime/laravel-sql-reporter)[![Build Status](https://github.com/onlime/laravel-sql-reporter/actions/workflows/ci.yml/badge.svg)](https://github.com/onlime/laravel-sql-reporter/actions/workflows/ci.yml)[![GitHub License](https://camo.githubusercontent.com/591b632479bea263897f809ffa9ae750e3f90538ef0f37e91d536e9772b5b58b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6f6e6c696d652f6c61726176656c2d73716c2d7265706f727465722e737667)](https://github.com/onlime/laravel-sql-reporter/blob/main/LICENSE)

This module allows you to log SQL queries to log file in Laravel framework. It's useful mainly when developing your application to verify whether your queries are valid and to make sure your application doesn't run too many or too slow database queries.

You may also use this in production as it should not cause a lot of overhead. Logged queries can be limited by query pattern, and logging only occurs at the end of each request or artisan command execution, not per query execution.

It reports a lot of metadata like total query count, total execution time, origin (request URL/console command), authenticated user, app environment, client browser agent / IP / hostname.

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

[](#installation)

1. Run

    ```
    $ composer require onlime/laravel-sql-reporter --dev
    ```

    in console to install this module (Notice `--dev` flag - it's recommended to use this package only for development).

    Laravel uses package auto-discovery, and it will automatically load this service provider, so you don't need to add anything into the `providers` section of `config/app.php`.
2. Run the following in your console to publish the default configuration file:

    ```
    $ php artisan vendor:publish --provider="Onlime\LaravelSqlReporter\Providers\ServiceProvider"
    ```

    By default, you should not edit published file because all the settings are loaded from `.env` file by default.
3. In your `.env` file add the following entries:

    ```
    SQL_REPORTER_DIRECTORY="logs/sql"
    SQL_REPORTER_USE_SECONDS=false
    SQL_REPORTER_CONSOLE_SUFFIX=
    SQL_REPORTER_LOG_EXTENSION=".sql"
    SQL_REPORTER_QUERIES_ENABLED=true
    SQL_REPORTER_QUERIES_OVERRIDE_LOG=false
    SQL_REPORTER_QUERIES_INCLUDE_PATTERN="#.*#i"
    SQL_REPORTER_QUERIES_EXCLUDE_PATTERN="/^\$/"
    SQL_REPORTER_QUERIES_REPORT_PATTERN='/^(?!select\s|start transaction|commit|(insert into|update|delete from) `(sessions|jobs|bans|logins)`).*/i'
    SQL_REPORTER_QUERIES_MIN_EXEC_TIME=0
    SQL_REPORTER_QUERIES_FILE_NAME="[Y-m]-log"
    SQL_REPORTER_FORMAT_HEADER_FIELDS="datetime,origin,status,user,env,agent,ip,host,referer"
    SQL_REPORTER_FORMAT_ENTRY_FORMAT="-- Query [query_nr] [[query_time]]\\n[query]"
    ```

    and adjust values to your needs. You can skip variables for which you want to use default values.

    To only log DML / modifying queries like `INSERT`, `UPDATE`, `DELETE`, but not logging any updates on `last_visit` or `remember_token`, I recommend to use:

    ```
    SQL_REPORTER_QUERIES_INCLUDE_PATTERN="/^(?!SELECT).*/i"
    SQL_REPORTER_QUERIES_EXCLUDE_PATTERN="/^UPDATE.*(last_visit|remember_token)/i"
    ```

    If you have also `.env.example` it's recommended to add those entries also in `.env.example` file just to make sure everyone knows about those env variables. Be aware that `SQL_REPORTER_DIRECTORY` is directory inside storage directory.

    To find out more about those setting please take a look at [Configuration file](config/sql-reporter.php)
4. Make sure directory specified in `.env` file exists in storage path, and you have valid permissions to create and modify files in this directory (If it does not exist this package will automatically create it when needed, but it's recommended to create it manually with valid file permissions)
5. Make sure on live server you will set logging SQL queries to false in your `.env` file: `SQL_REPORTER_QUERIES_ENABLED=false`. This package is recommended to be used only for development to not impact production application performance.

Optional
--------

[](#optional)

### GeoIP support

[](#geoip-support)

For optional GeoIP support (adding country information to client IP in log headers), you may install [stevebauman/location](https://github.com/stevebauman/location) in your project:

```
$ composer require stevebauman/location
$ php artisan vendor:publish --provider="Stevebauman\Location\LocationServiceProvider"
```

It will be auto-detected, no configuration needed for this. If you wish to use a different driver than the default [IpApi](https://ip-api.com/), e.g. `MaxMind` make sure you correctly configure it according to the docs: [Available Drivers](https://github.com/stevebauman/location#available-drivers)

### `QueryLogWritten` event

[](#querylogwritten-event)

This package fires a `QueryLogWritten` event after the log file has been written. You may use this event to further debug or analyze the logged queries in your application. The queries are filtered by the `SQL_REPORTER_QUERIES_REPORT_PATTERN` setting, which comes with a sensible default to exclude `SELECT` queries and some default tables like `sessions`, `jobs`, `bans`, `logins`. If you don't want to filter any queries, you may leave this setting empty.

In addition to the pattern, you may also configure a callback to define your own custom filtering logic, for example, in your `AppServiceProvider`:

```
use Onlime\LaravelSqlReporter\SqlQuery;
use Onlime\LaravelSqlReporter\Writer;

Writer::shouldReportQuery(function (SqlQuery $query) {
    // Only include queries in the `QueryLogWritten` event that took longer than 100ms
    return $query->time > 100;
});
```

With the `SqlQuery` object, you have access to both `$rawQuery` and the (unprepared) `$query`/`$bindings`. The filter possibilities by providing a callback to `Writer::shouldReportQuery()` are endless!

Development
-----------

[](#development)

Checkout project and run tests:

```
$ git clone https://github.com/onlime/laravel-sql-reporter.git
$ cd laravel-sql-reporter
$ composer install

# run both Feature and Unit tests
$ vendor/bin/pest
# run unit tests with coverage report
$ vendor/bin/pest --coverage
```

FAQ
---

[](#faq)

### How does this package differ from `mnabialek/laravel-sql-logger` ?

[](#how-does-this-package-differ-from-mnabialeklaravel-sql-logger-)

This package was inspired by [mnabialek/laravel-sql-logger](https://github.com/mnabialek/laravel-sql-logger) and basically does the same thing: Logging your SQL queries. Here's the difference:

- Query logging is not triggered upon each query execution but instead at a final step, using `RequestHandled` and `CommandFinished` events.
- This allows us to include much more information about the whole query executions like total query count, total execution time, and very detailed header information like origin (request URL/console command), authenticated user, app environment, client browser agent / IP / hostname.
- This package is greatly simplified and only provides support for Laravel 10+ / PHP 8.2+
- It uses the Laravel built-in query logging (`DB::enableQueryLog()`) which logs all queries in memory, which should perform much better than writing every single query to the log file.
- By default, `onlime/laravel-sql-reporter` produces much nicer log output, especially since we only write header information before the first query.

Sample log output:

```
-- --------------------------------------------------
-- Datetime: 2024-03-11 09:33:22
-- Origin:   (request) GET http://localhost:8000/demo
-- Status:   Executed 3 queries in 1.85ms
-- User:     testuser@example.com
-- Guard:    web
-- Env:      local
-- Agent:    Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:123.0) Gecko/20100101 Firefox/123.0
-- Ip:       127.0.0.1
-- Host:     localhost
-- Referer:
-- --------------------------------------------------
-- Query 1 [1.45ms]
select * from `users` where `id` = 1 limit 1;
-- Query 3 [0.4ms]
update `users` set `last_visit` = '2021-05-28 15:24:46' where `id` = 1;

```

In comparison, sample log output of `mnabialek/laravel-sql-logger`:

```
/*==================================================*/
/* Origin (request): GET http://localhost:8000/mail/api/user
   Query 1 - 2021-05-20 21:00:08 [1.4ms] */
select * from `users` where `id` = 1 limit 1;
/*==================================================*/
/* Origin (request): GET http://localhost:8000/mail/api/user
   Query 2 - 2021-05-20 21:00:08 [4.72ms] */
update `users` set `last_visit` = '2021-05-20 21:00:08' where `id` = 1;

```

Authors
-------

[](#authors)

Author of this awesome package is **[Philip Iezzi (Onlime GmbH)](https://www.onlime.ch/)**.

Large parts of this package were ported from the original [mnabialek/laravel-sql-logger](https://github.com/mnabialek/laravel-sql-logger). Credits go to **[Marcin Nabiałek](http://marcin.nabialek.org/en/)**. Please star his great package on GitHub! You may use `composer thanks` for this.

Changes
-------

[](#changes)

All changes are listed in [CHANGELOG](CHANGELOG.md)

Caveats
-------

[](#caveats)

- If your application crashes, this package will not log any queries, as logging is only triggered at the end of the request cycle. As alternative, you could use [mnabialek/laravel-sql-logger](https://github.com/mnabialek/laravel-sql-logger) which triggers sql logging on each query execution.
- It's currently not possible to log slow queries into a separate logfile. I wanted to keep that package simple.

TODO
----

[](#todo)

- Improve unit testing to reach 100% coverage
- Integrate Coveralls.io and add test coverage status badge to README
- Add browser type information to log headers, maybe using [hisorange/browser-detect](https://github.com/hisorange/browser-detect)

License
-------

[](#license)

This package is licenced under the [MIT license](LICENSE) however support is more than welcome.

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance88

Actively maintained with recent releases

Popularity25

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 85.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 ~116 days

Recently: every ~84 days

Total

16

Last Release

61d ago

Major Versions

v0.9.1 → v1.0.02022-02-10

PHP version history (5 changes)v0.9PHP ^8.0

v1.0.1PHP ^8.1

v1.2.2PHP ^8.2

v1.3.4PHP ^8.2 || ^8.3 || ^8.4 || ^8.5

v1.3.6PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/4ff0437baf11ae7c47632a6b518bbdde5df2ee9fbd90f2e112c64b97b19d078b?d=identicon)[onlime](/maintainers/onlime)

---

Top Contributors

[![onlime](https://avatars.githubusercontent.com/u/2759561?v=4)](https://github.com/onlime "onlime (74 commits)")[![pascalbaljet](https://avatars.githubusercontent.com/u/8403149?v=4)](https://github.com/pascalbaljet "pascalbaljet (13 commits)")

---

Tags

loglaravelsqlloggersql logsql loggerlog sql querieslog sql

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/onlime-laravel-sql-reporter/health.svg)

```
[![Health](https://phpackages.com/badges/onlime-laravel-sql-reporter/health.svg)](https://phpackages.com/packages/onlime-laravel-sql-reporter)
```

###  Alternatives

[mnabialek/laravel-sql-logger

Log SQL queries in Laravel/Lumen framework

158796.3k2](/packages/mnabialek-laravel-sql-logger)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[danielme85/laravel-log-to-db

Custom Laravel Log channel handler that can store log events to SQL or MongoDB databases. Uses Laravel native logging functionality.

135934.5k1](/packages/danielme85-laravel-log-to-db)[klaravel/db-logger

Laravel database/sql queries logger.

132.4k](/packages/klaravel-db-logger)[cybercog/laravel-clickhouse

ClickHouse migrations for Laravel

163166.8k](/packages/cybercog-laravel-clickhouse)

PHPackages © 2026

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