Skip to content

Commit be838e8

Browse files
author
Nazar Saikevych
committed
init src
1 parent 8567b33 commit be838e8

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
/.idea
3+
/node_modules
4+
/composer.lock

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Lemberg
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 all
13+
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 THE
21+
SOFTWARE.
22+

composer.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "lemberg/laravel-query-log",
3+
"description": "A query log",
4+
"license": "MIT",
5+
"keywords": [
6+
"query",
7+
"log"
8+
],
9+
"type": "library",
10+
"authors": [
11+
{
12+
"name": "Nazar Saikevych",
13+
"email": "[email protected]"
14+
}
15+
],
16+
"require": {
17+
"php": ">=7.0"
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"Lemberg\\LaravelQueryLog\\": "src"
22+
}
23+
},
24+
"extra": {
25+
"laravel": {
26+
"providers": [
27+
"Lemberg\\LaravelQueryLog\\ServiceProvider"
28+
]
29+
}
30+
},
31+
"config": {
32+
"preferred-install": "dist",
33+
"sort-packages": true,
34+
"optimize-autoloader": true
35+
}
36+
}

config/query-log.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return [
4+
'active' => env('QUERY_LOG', false),
5+
'path' => storage_path('/logs/sql.log'),
6+
];

src/ServiceProvider.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Lemberg\LaravelQueryLog;
4+
5+
use Illuminate\Support\Facades\DB;
6+
use Monolog\Handler\StreamHandler;
7+
use Monolog\Logger;
8+
9+
class ServiceProvider extends \Illuminate\Support\ServiceProvider
10+
{
11+
const CONFIG_PATH = __DIR__ . '/../config/query-log.php';
12+
13+
public function boot()
14+
{
15+
$this->publishes([
16+
self::CONFIG_PATH => config_path('query-log.php'),
17+
], 'config');
18+
19+
$this->writeQueryLog();
20+
}
21+
22+
public function register()
23+
{
24+
$this->mergeConfigFrom(
25+
self::CONFIG_PATH,
26+
'query-log'
27+
);
28+
}
29+
30+
protected function writeQueryLog()
31+
{
32+
if (config('query-log.active') === true) {
33+
DB::listen(function ($query) {
34+
// instance of Monolog
35+
$monolog = new Logger('query_logs');
36+
37+
// Choose FirePHP as the log handler
38+
$monolog->pushHandler(new StreamHandler(config('query-log.path'), Logger::DEBUG));
39+
40+
// To get the full sql query with bindings inserted
41+
$sql = str_replace(array('%', '?'), array('%%', '%s'), $query->sql);
42+
$fullSql = vsprintf($sql, $query->bindings);
43+
44+
// Start logging
45+
$monolog->addInfo('SQL', array('time' => $query->time, 'query' => $fullSql));
46+
});
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)