|
| 1 | +# Laravel SQL Reporter |
| 2 | + |
| 3 | +[](https://packagist.org/packages/onlime/laravel-sql-reporter) |
| 4 | +[](https://travis-ci.org/onlime/laravel-sql-reporter) |
| 5 | +[](https://coveralls.io/github/onlime/laravel-sql-reporter) |
| 6 | +[](https://scrutinizer-ci.com/g/onlime/laravel-sql-reporter/) |
| 7 | + |
| 8 | +This module allows you to log SQL queries (and slow SQL queries) to log file in Laravel framework. It's useful mainly |
| 9 | +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. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +1. Run |
| 14 | + ```bash |
| 15 | + $ composer require onlime/laravel-sql-reporter --dev |
| 16 | + ``` |
| 17 | + in console to install this module (Notice `--dev` flag - it's recommended to use this package only for development). |
| 18 | + |
| 19 | + 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`. |
| 20 | + |
| 21 | +2. Run the following in your console to publish the default configuration file: |
| 22 | + |
| 23 | + ```bash |
| 24 | + $ php artisan vendor:publish --provider="Onlime\LaravelSqlReporter\Providers\ServiceProvider" |
| 25 | + ``` |
| 26 | + |
| 27 | + By default you should not edit published file because all the settings are loaded from `.env` file by default. |
| 28 | + |
| 29 | +3. In your .env file add the following entries: |
| 30 | + |
| 31 | + ```ini |
| 32 | + SQL_REPORTER_DIRECTORY="logs/sql" |
| 33 | + SQL_REPORTER_USE_SECONDS=false |
| 34 | + SQL_REPORTER_CONSOLE_SUFFIX= |
| 35 | + SQL_REPORTER_LOG_EXTENSION=".sql" |
| 36 | + SQL_REPORTER_QUERIES_ENABLED=true |
| 37 | + SQL_REPORTER_QUERIES_OVERRIDE_LOG=false |
| 38 | + SQL_REPORTER_QUERIES_PATTERN="#.*#i" |
| 39 | + SQL_REPORTER_QUERIES_MIN_EXEC_TIME=0 |
| 40 | + SQL_REPORTER_QUERIES_FILE_NAME="[Y-m]-log" |
| 41 | + SQL_REPORTER_FORMAT_HEADER_FIELDS="origin,datetime,status,user,env,agent,ip,host,referer" |
| 42 | + SQL_REPORTER_FORMAT_ENTRY_FORMAT="-- Query [query_nr] [[query_time]]\\n[query]" |
| 43 | + ``` |
| 44 | + |
| 45 | + and adjust values to your needs. You can skip variables for which you want to use default values. |
| 46 | + |
| 47 | + If you have also `.env.sample` it's also recommended to add those entries also in `.env.sample` file just to make sure everyone know about those env variables. Be aware that `SQL_REPORTER_DIRECTORY` is directory inside storage directory. If you want you can change it editing `config/sql-reporter.php` file. |
| 48 | + |
| 49 | + To find out more about those setting please take a look at [Configuration file](config/sql-reporter.php) |
| 50 | + |
| 51 | +4. Make sure directory specified in `.env` file exists in storage path and you have valid file 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) |
| 52 | + |
| 53 | +5. Make sure on live server you will set logging SQL queries to false in your `.env` file. This package is recommended to be used only for development to not impact production application performance. |
| 54 | + |
| 55 | +## Optional |
| 56 | + |
| 57 | +For optional GeoIP support (adding country information to client IP in log headers), you may install [torann/geoip](https://github.com/Torann/laravel-geoip) in your project: |
| 58 | + |
| 59 | +```bash |
| 60 | +$ composer require torann/geoip |
| 61 | +$ php artisan vendor:publish --provider="Torann\GeoIP\GeoIPServiceProvider" |
| 62 | +``` |
| 63 | + |
| 64 | +## FAQ |
| 65 | + |
| 66 | +### How does this package differ from `mnabialek/laravel-sql-logger` ? |
| 67 | + |
| 68 | +This package was inspired by [mnabialek/laravel-sql-logger](https://github.com/mnabialek/laravel-sql-logger) and basically, it does the same thing: Logging your SQL queries. Here's the difference: |
| 69 | +
|
| 70 | +- Query logging is not triggered upon each query execution but instead at a final step, using `RequestHandled` and `CommandFinished` events. |
| 71 | +- 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. |
| 72 | +- This package is greatly simplified and only provides support for Laravel 8+ / PHP 8 |
| 73 | +- 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. |
| 74 | +- By default, `onlime/laravel-sql-reporter` produces much nicer log output, especially since we only write header information before the first query. |
| 75 | +
|
| 76 | +Sample log output: |
| 77 | +
|
| 78 | +``` |
| 79 | +-- -------------------------------------------------- |
| 80 | +-- Datetime: 2021-05-28 15:24:46 |
| 81 | +-- Origin: (request) GET http://localhost:8000/demo |
| 82 | +-- Status: Executed 3 queries in 1.85ms |
| 83 | +-- User: |
| 84 | +-- Env: local |
| 85 | +-- Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:88.0) Gecko/20100101 Firefox/88.0 |
| 86 | +-- Ip: 127.0.0.1 |
| 87 | +-- Host: localhost |
| 88 | +-- Referer: |
| 89 | +-- -------------------------------------------------- |
| 90 | +-- Query 1 [1.45ms] |
| 91 | +select * from `users` where `id` = 1 limit 1; |
| 92 | +-- Query 3 [0.4ms] |
| 93 | +update `users` set `last_visit` = '2021-05-28 15:24:46' where `id` = 1; |
| 94 | +``` |
| 95 | +
|
| 96 | +In comparison, sample log output of `mnabialek/laravel-sql-logger`: |
| 97 | +
|
| 98 | +``` |
| 99 | +/*==================================================*/ |
| 100 | +/* Origin (request): GET http://localhost:8000/mail/api/user |
| 101 | + Query 1 - 2021-05-20 21:00:08 [1.4ms] */ |
| 102 | +select * from `users` where `id` = 1 limit 1; |
| 103 | +/*==================================================*/ |
| 104 | +/* Origin (request): GET http://localhost:8000/mail/api/user |
| 105 | + Query 2 - 2021-05-20 21:00:08 [4.72ms] */ |
| 106 | +update `users` set `last_visit` = '2021-05-20 21:00:08' where `id` = 1; |
| 107 | +``` |
| 108 | +
|
| 109 | +## Authors |
| 110 | +
|
| 111 | +Author of this awesome package is **[Philip Iezzi (Onlime GmbH)](https://www.onlime.ch/)**. |
| 112 | +
|
| 113 | +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/)**. |
| 114 | +Please star his great package on GitHub! You may use `composer thanks` for this. |
| 115 | +
|
| 116 | +## Changes |
| 117 | +
|
| 118 | +All changes are listed in [CHANGELOG](CHANGELOG.md) |
| 119 | +
|
| 120 | +## Todo |
| 121 | +
|
| 122 | +- [ ] Add browser type information to log headers, using hisorange/browser-detect |
| 123 | +
|
| 124 | +## License |
| 125 | +
|
| 126 | +This package is licenced under the [MIT license](LICENSE) however support is more than welcome. |
0 commit comments