1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mnabialek\LaravelSqlLogger; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Contracts\Foundation\Application; |
7
|
|
|
use Mnabialek\LaravelSqlLogger\Objects\SqlQuery; |
8
|
|
|
|
9
|
|
|
class Writer |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Application |
13
|
|
|
*/ |
14
|
|
|
private $app; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Formatter |
18
|
|
|
*/ |
19
|
|
|
private $formatter; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Config |
23
|
|
|
*/ |
24
|
|
|
private $config; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Writer constructor. |
28
|
|
|
* |
29
|
|
|
* @param Application $app |
30
|
|
|
* @param Formatter $formatter |
31
|
|
|
* @param Config $config |
32
|
|
|
*/ |
33
|
|
|
public function __construct(Application $app, Formatter $formatter, Config $config) |
34
|
|
|
{ |
35
|
|
|
$this->app = $app; |
36
|
|
|
$this->formatter = $formatter; |
37
|
|
|
$this->config = $config; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Save queries to log. |
42
|
|
|
* |
43
|
|
|
* @param SqlQuery $query |
44
|
|
|
*/ |
45
|
|
|
public function save(SqlQuery $query) |
46
|
|
|
{ |
47
|
|
|
$this->createDirectoryIfNotExists($query->number()); |
48
|
|
|
|
49
|
|
|
$line = $this->formatter->getLine($query); |
50
|
|
|
|
51
|
|
|
if ($this->config->logQueries()) { |
52
|
|
|
$this->saveLine($line, $this->logName(), $this->shouldOverrideFile($query)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($this->shouldLogSlowQuery($query->time())) { |
56
|
|
|
$this->saveLine($line, $this->slowLogName()); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Create directory if it does not exist yet. |
62
|
|
|
* |
63
|
|
|
* @param int $queryNumber |
64
|
|
|
*/ |
65
|
|
|
protected function createDirectoryIfNotExists($queryNumber) |
66
|
|
|
{ |
67
|
|
|
if ($queryNumber == 1 && ! file_exists($directory = $this->directory())) { |
68
|
|
|
mkdir($directory, 0777, true); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get directory where file should be located. |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
protected function directory() |
78
|
|
|
{ |
79
|
|
|
return rtrim($this->config->logDirectory(), '\\/'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get log name. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
protected function logName() |
88
|
|
|
{ |
89
|
|
|
return Carbon::now()->format('Y-m-d') . $this->suffix() . '-log.sql'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get slow log name. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
protected function slowLogName() |
98
|
|
|
{ |
99
|
|
|
return Carbon::now()->format('Y-m-d') . $this->suffix() . '-slow-log.sql'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get file suffix. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
protected function suffix() |
108
|
|
|
{ |
109
|
|
|
return $this->config->separateConsoleLogs() && $this->app->runningInConsole() ? '-artisan' : ''; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Verify whether slow query should be logged. |
114
|
|
|
* |
115
|
|
|
* @param float $execTime |
116
|
|
|
* |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
protected function shouldLogSlowQuery($execTime) |
120
|
|
|
{ |
121
|
|
|
return $this->config->logSlowQueries() && $execTime >= $this->config->slowLogTime(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Save data to log file. |
126
|
|
|
* |
127
|
|
|
* @param string $line |
128
|
|
|
* @param string $fileName |
129
|
|
|
* @param bool $override |
130
|
|
|
*/ |
131
|
|
|
protected function saveLine($line, $fileName, $override = false) |
132
|
|
|
{ |
133
|
|
|
file_put_contents($this->directory() . DIRECTORY_SEPARATOR . $fileName, |
134
|
|
|
$line, $override ? 0 : FILE_APPEND); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Verify whether file should be overridden. |
139
|
|
|
* |
140
|
|
|
* @param SqlQuery $query |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
private function shouldOverrideFile(SqlQuery $query) |
145
|
|
|
{ |
146
|
|
|
return ($query->number() == 1 && $this->config->overrideFile()); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|