Skip to content

Commit 29d9359

Browse files
committed
Initial commit
0 parents  commit 29d9359

File tree

14 files changed

+1115
-0
lines changed

14 files changed

+1115
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/.phpunit.result.cache
2+
/vendor/
3+
/.idea
4+
/composer.lock
5+
6+
# TODO: fix tests and add them to repo
7+
/tests
8+
/phpunit.xml

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Onlime GmbH, Philip Iezzi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Laravel SQL Reporter
2+
3+
[![Packagist](https://img.shields.io/packagist/dt/onlime/laravel-sql-reporter.svg)](https://packagist.org/packages/onlime/laravel-sql-reporter)
4+
[![Build Status](https://travis-ci.org/onlime/laravel-sql-reporter.svg?branch=master)](https://travis-ci.org/onlime/laravel-sql-reporter)
5+
[![Coverage Status](https://coveralls.io/repos/github/onlime/laravel-sql-reporter/badge.svg)](https://coveralls.io/github/onlime/laravel-sql-reporter)
6+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/onlime/laravel-sql-reporter/badges/quality-score.png?b=master)](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.

composer.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "onlime/laravel-sql-reporter",
3+
"description": "Log SQL queries in Laravel framework",
4+
"keywords": ["laravel", "sql", "log", "logger", "sql logger", "log sql queries", "log sql", "sql log"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Philip Iezzi",
9+
"email": "philip@onlime.ch"
10+
},
11+
{
12+
"name": "Onlime GmbH",
13+
"homepage": "https://www.onlime.ch/"
14+
}
15+
],
16+
"require": {
17+
"php": "^8.0",
18+
"illuminate/support": "8.*",
19+
"nesbot/carbon": "^2.0",
20+
"illuminate/filesystem": "8.*",
21+
"illuminate/container": "8.*"
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": "^9.5",
25+
"mockery/mockery": "^1.0",
26+
"laravel/framework": "8.*"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"Onlime\\LaravelSqlReporter\\": "src/"
31+
}
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"Onlime\\LaravelSqlReporter\\Tests\\": "tests"
36+
}
37+
},
38+
"extra": {
39+
"laravel": {
40+
"providers": [
41+
"Onlime\\LaravelSqlReporter\\Providers\\EventServiceProvider"
42+
]
43+
},
44+
"thanks": {
45+
"name": "mnabialek/laravel-version",
46+
"url": "https://github.com/mnabialek/laravel-version"
47+
},
48+
"suggest": {
49+
"torann/geoip": "GeoIP country detection for log headers information",
50+
"symfony/thanks": "Star packages you use running 'composer thanks' command"
51+
}
52+
}
53+
}

config/sql-reporter.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
return [
4+
5+
'general' => [
6+
/*
7+
* Directory where log files will be saved
8+
*/
9+
'directory' => storage_path(env('SQL_REPORTER_DIRECTORY', 'logs/sql')),
10+
11+
/*
12+
* Whether execution time in log file should be displayed in seconds
13+
* (by default it's in milliseconds)
14+
*/
15+
'use_seconds' => env('SQL_REPORTER_USE_SECONDS', false),
16+
17+
/*
18+
* Suffix for Artisan queries logs (if it's empty same logfile will be used for Artisan)
19+
*/
20+
'console_log_suffix' => env('SQL_REPORTER_CONSOLE_SUFFIX', ''),
21+
22+
/*
23+
* Extension for log files
24+
*/
25+
'extension' => env('SQL_REPORTER_LOG_EXTENSION', '.sql'),
26+
],
27+
28+
'formatting' => [
29+
/*
30+
* Header fields, comma-separated. Available options:
31+
*
32+
* - origin: where this query coming from - method/request or artisan command
33+
* - datetime: date and time when the first query was executed
34+
* - status: total query count and execution time for all queries
35+
* - user: username of authenticated user
36+
* - env: application environment
37+
* - agent: user agent
38+
* - ip: (request-only) remote user IP
39+
* - host: (request-only) remote user hostname (resolved IP)
40+
* - referer: (request-only) browser referer
41+
*
42+
* It does not harm to keep all fields enabled. The request-only fields would simply report some standard/empty
43+
* data if origin is an Artisan command.
44+
*/
45+
'header_fields' => array_filter(explode(',', env('SQL_REPORTER_FORMAT_HEADER_FIELDS',
46+
'origin,datetime,status,user,env,agent,ip,host,referer'
47+
))),
48+
49+
/*
50+
* Single entry format. Available options:
51+
*
52+
* - [query_nr] - query number
53+
* - [datetime] - date and time when query was executed
54+
* - [query_time] - how long query was executed
55+
* - [query] - query itself
56+
* - [separator] - extra separator line to make it easier to see where next query starts
57+
* - \n - new line separator.
58+
*/
59+
'entry_format' => env('SQL_REPORTER_FORMAT_ENTRY_FORMAT',
60+
"-- Query [query_nr] [[query_time]]\n[query]"
61+
),
62+
],
63+
64+
'queries' => [
65+
/*
66+
* Whether all SQL queries should be logged
67+
*/
68+
'enabled' => env('SQL_REPORTER_QUERIES_ENABLED', true),
69+
70+
/*
71+
* Whether log (for all queries, not for slow queries) should be overridden.
72+
* It might be useful when you test some functionality and you want to
73+
* compare your queries (or number of queries) - be aware that when using
74+
* AJAX it will override your log file in each request
75+
*/
76+
'override_log' => env('SQL_REPORTER_QUERIES_OVERRIDE_LOG', false),
77+
78+
/*
79+
* Pattern that should be matched to log query. By default all queries are logged.
80+
*
81+
* examples:
82+
* '#.*#i' will log all queries
83+
* '/^SELECT.*$/i' will log only SELECT queries
84+
* '/^(?!SELECT).*$/i' will log all queries other than SELECT (modifying queries)
85+
*/
86+
'pattern' => env('SQL_REPORTER_QUERIES_PATTERN', '/.*/i'),
87+
88+
/*
89+
* Only log queries with slow execution time (in milliseconds)
90+
*/
91+
'min_exec_time' => env('SQL_REPORTER_QUERIES_MIN_EXEC_TIME', 0),
92+
93+
/*
94+
* Log file name without extension - elements between [ and ] characters will be parsed
95+
* according to format used by https://www.php.net/manual/en/function.date.php
96+
*
97+
* examples:
98+
* '[Y-m]-log' (default) results in YYYY-MM-log.sql logfile (monthly rotations)
99+
* '[Y-m-d]-log' results in YYYY-MM-DD-log.sql logfile (daily rotations)
100+
* 'query-log' results in query-log.sql (you should use logrotate for rotation)
101+
*/
102+
'file_name' => env('SQL_REPORTER_QUERIES_FILE_NAME', '[Y-m]-log'),
103+
],
104+
];

0 commit comments

Comments
 (0)