├── .gitignore
├── README.md
├── composer.json
└── src
├── Contracts
└── FileWritable.php
├── QueryLog.php
├── QueryLogServiceProvider.php
└── Supports
├── JsonLogFileWriter.php
└── TextLogFileWriter.php
/.gitignore:
--------------------------------------------------------------------------------
1 | /vendor
2 | /.idea
3 | /node_modules
4 | /composer.lock
5 | *.txt
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
Laravel Query Log
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | Log executed Laravel SQL queries and their line number and more!
10 |
11 |
12 | ## Support
13 |
14 |
15 | ## Documentation
16 | Get documentation on [laravelarticle.com](https://laravelarticle.com/laravel-query-log)
17 |
18 | ## Sample Preview
19 | text format
20 | ```text
21 | url : http://example.test
22 | method : GET
23 | total_query : 3
24 | total_time : 5.69
25 | --------------------
26 |
27 | sl : 1
28 | query : select * from `students` where `id` = ? and `name` = ?
29 | bindings : 6|Tomas
30 | final_query : select * from `students` where `id` = 6 and `name` = 'Tomas'
31 | time : 4.38
32 | file : C:\www\example\app\Http\Controllers\SiteController.php:10
33 | line : 10
34 |
35 | sl : 2
36 | query : select * from `students` where `id` = ? and `phone` = ?
37 | bindings : 6|649-768-7305
38 | final_query : select * from `students` where `id` = 6 and `phone` = '649-768-7305'
39 | time : 0.75
40 | file : C:\www\example\app\Http\Controllers\SiteController.php:11
41 | line : 11
42 |
43 | sl : 3
44 | query : select * from `students` where `id` = ?
45 | bindings : 6
46 | final_query : select * from `students` where `id` = 6
47 | time : 0.56
48 | file : C:\www\example\app\Http\Controllers\SiteController.php:12
49 | line : 12
50 | ```
51 |
52 | ## Other Packages
53 | - [Laravel Log Reader](https://github.com/haruncpi/laravel-log-reader) - A simple and beautiful laravel log reader.
54 | - [Laravel H](https://github.com/haruncpi/laravel-h) - A helper package for Laravel Framework.
55 | - [Laravel Simple Filemanager](https://github.com/haruncpi/laravel-simple-filemanager) - A simple filemanager for Laravel.
56 | - [Laravel Option Framework](https://github.com/haruncpi/laravel-option-framework) - Option framework for Laravel.
57 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "haruncpi/laravel-query-log",
3 | "description": "Log your all laravel sql query",
4 | "license": "cc-by-4.0",
5 | "authors": [{
6 | "name": "Md.Harun-Ur-Rashid",
7 | "email": "harun.cox@gmail.com"
8 | }],
9 | "minimum-stability": "dev",
10 | "prefer-stable": true,
11 | "require": {
12 | "php": ">=5.6.0"
13 | },
14 | "autoload": {
15 | "psr-4": {
16 | "Haruncpi\\QueryLog\\": "src/"
17 | }
18 | },
19 | "extra": {
20 | "laravel": {
21 | "providers": [
22 | "Haruncpi\\QueryLog\\QueryLogServiceProvider"
23 | ]
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/Contracts/FileWritable.php:
--------------------------------------------------------------------------------
1 | file_path = storage_path($this->file_name);
54 | $this->format = trim(env('QUERY_LOG_FORMAT', self::FORMAT_TEXT));
55 | $this->total_query = 0;
56 | $this->total_time = 0;
57 | $this->final = [];
58 |
59 | if (!in_array(strtolower($this->format), [self::FORMAT_TEXT, self::FORMAT_JSON])) {
60 | throw new \Exception('Invalid query log data file format. Support text or json file format.');
61 | }
62 |
63 | if (file_exists($this->file_path)) {
64 | unlink($this->file_path);
65 | }
66 |
67 |
68 | $this->listenQueries();
69 | }
70 |
71 |
72 | /**
73 | * Query listener
74 | * @return void
75 | *
76 | * @since 1.0.0
77 | */
78 | private function listenQueries()
79 | {
80 |
81 | DB::listen(function ($query) {
82 | $this->total_query++;
83 | $this->total_time += $query->time;
84 |
85 | $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
86 | foreach ($backtrace as $trace) {
87 | if (array_key_exists('file', $trace) && array_key_exists('line', $trace)) {
88 | if (strpos($trace['file'], base_path('app')) !== false) {
89 | $this->addQuery($query, $trace);
90 | break;
91 | }
92 | }
93 | }
94 | });
95 |
96 | app()->terminating(function () {
97 |
98 | $this->final['meta'] = [
99 | 'url' => request()->url(),
100 | 'method' => request()->method(),
101 | 'total_query' => $this->total_query,
102 | 'total_time' => $this->total_time
103 | ];
104 |
105 | if ($this->format == self::FORMAT_JSON && isset($this->final['queries'])) {
106 | (new JsonLogFileWriter)->write($this->file_path, $this->final);
107 | }
108 |
109 | if ($this->format == self::FORMAT_TEXT && isset($this->final['queries'])) {
110 | (new TextLogFileWriter)->write($this->file_path, $this->final);
111 | }
112 | });
113 | }
114 |
115 | /**
116 | * Make final query from sql bindings
117 | *
118 | * @param $query
119 | * @return string
120 | *
121 | * @since 1.0.0
122 | */
123 | private function getSqlWithBindings($query)
124 | {
125 | return vsprintf(str_replace('?', '%s', $query->sql), collect($query->bindings)
126 | ->map(function ($binding) {
127 | return is_numeric($binding) ? $binding : "'{$binding}'";
128 | })->toArray());
129 | }
130 |
131 |
132 | /**
133 | * add each query in a specific array format
134 | *
135 | * @param object $query
136 | * @param array $trace
137 | * @return void
138 | *
139 | * @since 1.0.0
140 | */
141 | private function addQuery($query, $trace)
142 | {
143 | $queryStr = $this->getSqlWithBindings($query);
144 | $time = $query->time;
145 | $file = $trace['file'];
146 | $line = $trace['line'];
147 |
148 |
149 | $this->final['queries'][] = [
150 | 'sl' => $this->total_query,
151 | 'query' => $query->sql,
152 | 'bindings' => $query->bindings,
153 | 'final_query' => $queryStr,
154 | 'time' => $time,
155 | 'file' => $file . ":$line",
156 | 'line' => $line
157 | ];
158 | }
159 | }
160 |
--------------------------------------------------------------------------------
/src/QueryLogServiceProvider.php:
--------------------------------------------------------------------------------
1 | file_path = $file_path;
12 |
13 | foreach ($data['meta'] as $key => $value) {
14 | $this->writeLine($this->addSpace($key, 12) . ": $value");
15 | }
16 |
17 | $this->writeLine("--------------------\n");
18 |
19 | foreach ($data['queries'] as $q) {
20 | foreach ($q as $key => $val) {
21 | if (is_array($val)) {
22 | $this->writeLine($this->addSpace($key, 12) . ": " . implode("|", $val));
23 | } else {
24 | $this->writeLine($this->addSpace($key, 12) . ": " . $val);
25 | }
26 | }
27 |
28 | $this->writeLine("");
29 | }
30 | }
31 |
32 | private function addSpace($key, $max)
33 | {
34 | return $key . str_repeat(' ', $max - strlen($key));
35 | }
36 |
37 | private function writeLine($txt)
38 | {
39 | file_put_contents($this->file_path, $txt . "\n", FILE_APPEND);
40 | }
41 | }
--------------------------------------------------------------------------------