1
|
|
|
<?php namespace Arcanedev\LogViewer\Utilities; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LogViewer\Contracts\FilesystemInterface; |
4
|
|
|
use Arcanedev\LogViewer\Contracts\LogCheckerInterface; |
5
|
|
|
use Illuminate\Contracts\Config\Repository as Config; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class LogChecker |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\LogViewer\Utilities |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
* |
13
|
|
|
* @todo Adding the translation or not ?? |
14
|
|
|
*/ |
15
|
|
|
class LogChecker implements LogCheckerInterface |
16
|
|
|
{ |
17
|
|
|
/* ------------------------------------------------------------------------------------------------ |
18
|
|
|
| Constants |
19
|
|
|
| ------------------------------------------------------------------------------------------------ |
20
|
|
|
*/ |
21
|
|
|
/** |
22
|
|
|
* @link http://laravel.com/docs/5.1/errors#configuration |
23
|
|
|
* @link https://github.com/Seldaek/monolog/blob/master/doc/02-handlers-formatters-processors.md#log-to-files-and-syslog |
24
|
|
|
*/ |
25
|
|
|
const HANDLER_DAILY = 'daily'; |
26
|
|
|
const HANDLER_SINGLE = 'single'; |
27
|
|
|
const HANDLER_SYSLOG = 'syslog'; |
28
|
|
|
const HANDLER_ERRORLOG = 'errorlog'; |
29
|
|
|
|
30
|
|
|
/* ------------------------------------------------------------------------------------------------ |
31
|
|
|
| Properties |
32
|
|
|
| ------------------------------------------------------------------------------------------------ |
33
|
|
|
*/ |
34
|
|
|
/** |
35
|
|
|
* The config repository instance. |
36
|
|
|
* |
37
|
|
|
* @var \Illuminate\Contracts\Config\Repository |
38
|
|
|
*/ |
39
|
|
|
private $config; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The filesystem instance. |
43
|
|
|
* |
44
|
|
|
* @var \Arcanedev\LogViewer\Contracts\FilesystemInterface |
45
|
|
|
*/ |
46
|
|
|
private $filesystem; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Log handler mode. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $handler = ''; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The check status. |
57
|
|
|
* |
58
|
|
|
* @var bool |
59
|
|
|
*/ |
60
|
|
|
private $status = true; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The check messages. |
64
|
|
|
* |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
private $messages; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Log files status (or statuses... i don't know). |
71
|
|
|
* |
72
|
|
|
* @var array |
73
|
|
|
*/ |
74
|
|
|
private $files; |
75
|
|
|
|
76
|
|
|
/* ------------------------------------------------------------------------------------------------ |
77
|
|
|
| Constructor |
78
|
|
|
| ------------------------------------------------------------------------------------------------ |
79
|
|
|
*/ |
80
|
|
|
/** |
81
|
|
|
* Make LogChecker instance. |
82
|
|
|
* |
83
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config |
84
|
|
|
* @param \Arcanedev\LogViewer\Contracts\FilesystemInterface $filesystem |
85
|
|
|
*/ |
86
|
18 |
|
public function __construct(Config $config, FilesystemInterface $filesystem) |
87
|
|
|
{ |
88
|
18 |
|
$this->setConfig($config); |
89
|
18 |
|
$this->setFilesystem($filesystem); |
90
|
18 |
|
$this->files = []; |
91
|
|
|
|
92
|
18 |
|
$this->refresh(); |
93
|
18 |
|
} |
94
|
|
|
|
95
|
|
|
/* ------------------------------------------------------------------------------------------------ |
96
|
|
|
| Getters & Setters |
97
|
|
|
| ------------------------------------------------------------------------------------------------ |
98
|
|
|
*/ |
99
|
|
|
/** |
100
|
|
|
* Set the config instance. |
101
|
|
|
* |
102
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config |
103
|
|
|
* |
104
|
|
|
* @return self |
105
|
|
|
*/ |
106
|
18 |
|
public function setConfig(Config $config) |
107
|
|
|
{ |
108
|
18 |
|
$this->config = $config; |
109
|
|
|
|
110
|
18 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set the Filesystem instance. |
115
|
|
|
* |
116
|
|
|
* @param \Arcanedev\LogViewer\Contracts\FilesystemInterface $filesystem |
117
|
|
|
* |
118
|
|
|
* @return self |
119
|
|
|
*/ |
120
|
18 |
|
public function setFilesystem(FilesystemInterface $filesystem) |
121
|
|
|
{ |
122
|
18 |
|
$this->filesystem = $filesystem; |
123
|
|
|
|
124
|
18 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Set the log handler mode. |
129
|
|
|
* |
130
|
|
|
* @param string $handler |
131
|
|
|
* |
132
|
|
|
* @return self |
133
|
|
|
*/ |
134
|
18 |
|
protected function setHandler($handler) |
135
|
|
|
{ |
136
|
18 |
|
$this->handler = strtolower($handler); |
137
|
|
|
|
138
|
18 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/* ------------------------------------------------------------------------------------------------ |
142
|
|
|
| Main Functions |
143
|
|
|
| ------------------------------------------------------------------------------------------------ |
144
|
|
|
*/ |
145
|
|
|
/** |
146
|
|
|
* Get messages. |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
6 |
|
public function messages() |
151
|
|
|
{ |
152
|
6 |
|
$this->refresh(); |
153
|
|
|
|
154
|
6 |
|
return $this->messages; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Check if the check passes. |
159
|
|
|
* |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
3 |
|
public function passes() |
163
|
|
|
{ |
164
|
3 |
|
$this->refresh(); |
165
|
|
|
|
166
|
3 |
|
return $this->status; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Check if the check fails. |
171
|
|
|
* |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
3 |
|
public function fails() |
175
|
|
|
{ |
176
|
3 |
|
return ! $this->passes(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get the requirements. |
181
|
|
|
* |
182
|
|
|
* @return array |
183
|
|
|
*/ |
184
|
9 |
|
public function requirements() |
185
|
|
|
{ |
186
|
9 |
|
$this->refresh(); |
187
|
|
|
|
188
|
9 |
|
if ($this->isDaily()) { |
189
|
|
|
return [ |
190
|
6 |
|
'status' => 'success', |
191
|
6 |
|
'header' => 'Application requirements fulfilled.', |
192
|
6 |
|
'message' => 'Are you ready to rock ?', |
193
|
6 |
|
]; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return [ |
197
|
3 |
|
'status' => 'failed', |
198
|
3 |
|
'header' => 'Application requirements failed.', |
199
|
3 |
|
'message' => $this->messages['handler'] |
200
|
3 |
|
]; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/* ------------------------------------------------------------------------------------------------ |
204
|
|
|
| Check Functions |
205
|
|
|
| ------------------------------------------------------------------------------------------------ |
206
|
|
|
*/ |
207
|
|
|
/** |
208
|
|
|
* Is a daily handler mode ? |
209
|
|
|
* |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
18 |
|
protected function isDaily() |
213
|
|
|
{ |
214
|
18 |
|
return $this->isSameHandler(self::HANDLER_DAILY); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Is the handler is the same as the application log handler. |
219
|
|
|
* |
220
|
|
|
* @param string $handler |
221
|
|
|
* |
222
|
|
|
* @return bool |
223
|
|
|
*/ |
224
|
18 |
|
private function isSameHandler($handler) |
225
|
|
|
{ |
226
|
18 |
|
return $this->handler === $handler; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/* ------------------------------------------------------------------------------------------------ |
230
|
|
|
| Other Functions |
231
|
|
|
| ------------------------------------------------------------------------------------------------ |
232
|
|
|
*/ |
233
|
|
|
/** |
234
|
|
|
* Refresh the checks. |
235
|
|
|
* |
236
|
|
|
* @return self |
237
|
|
|
*/ |
238
|
18 |
|
private function refresh() |
239
|
|
|
{ |
240
|
18 |
|
$this->setHandler($this->config->get('app.log', 'single')); |
241
|
|
|
|
242
|
18 |
|
$this->messages = [ |
243
|
18 |
|
'handler' => '', |
244
|
18 |
|
'files' => [], |
245
|
|
|
]; |
246
|
18 |
|
$this->files = []; |
247
|
|
|
|
248
|
18 |
|
$this->checkHandler(); |
249
|
18 |
|
$this->checkLogFiles(); |
250
|
|
|
|
251
|
18 |
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Check the handler mode |
256
|
|
|
*/ |
257
|
18 |
|
private function checkHandler() |
258
|
|
|
{ |
259
|
18 |
|
if ($this->isDaily()) { |
260
|
18 |
|
return; |
261
|
|
|
} |
262
|
|
|
|
263
|
3 |
|
$this->messages['handler'] = implode(' ', [ |
264
|
3 |
|
'You should set the log handler to `daily` mode.', |
265
|
|
|
'Please check the LogViwer wiki page (Requirements) for more details.' |
266
|
3 |
|
]); |
267
|
3 |
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Check all log files. |
271
|
|
|
* |
272
|
|
|
* @return array |
273
|
|
|
*/ |
274
|
18 |
|
private function checkLogFiles() |
275
|
|
|
{ |
276
|
18 |
|
foreach ($this->filesystem->all() as $path) { |
277
|
18 |
|
$this->checkLogFile($path); |
278
|
18 |
|
} |
279
|
18 |
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Check a log file. |
283
|
|
|
* |
284
|
|
|
* @param string $path |
285
|
|
|
*/ |
286
|
18 |
|
private function checkLogFile($path) |
287
|
|
|
{ |
288
|
18 |
|
$status = true; |
289
|
18 |
|
$file = basename($path); |
290
|
18 |
|
$message = "The log file [$file] is valid."; |
291
|
|
|
|
292
|
18 |
|
if ($this->isSingleLogFile($file)) { |
293
|
18 |
|
$this->status = $status = false; |
294
|
18 |
|
$this->messages['files'][$file] = $message = |
295
|
18 |
|
"You have a single log file in your application, you should split the [$file] into seperate log files."; |
296
|
18 |
|
} |
297
|
18 |
|
elseif ($this->isInvalidLogDate($file)) { |
298
|
18 |
|
$this->status = $status = false; |
299
|
18 |
|
$this->messages['files'][$file] = $message = |
300
|
18 |
|
"The log file [$file] has an invalid date, the format must be like laravel-YYYY-MM-DD.log."; |
301
|
18 |
|
} |
302
|
|
|
|
303
|
|
|
|
304
|
18 |
|
$this->files[$file] = compact('filename', 'status', 'message', 'path'); |
305
|
18 |
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Check if it's not a single log file. |
309
|
|
|
* |
310
|
|
|
* @param string $file |
311
|
|
|
* |
312
|
|
|
* @return bool |
313
|
|
|
*/ |
314
|
18 |
|
private function isSingleLogFile($file) |
315
|
|
|
{ |
316
|
18 |
|
return $file === 'laravel.log'; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Check the date of the log file. |
321
|
|
|
* |
322
|
|
|
* @param string $file |
323
|
|
|
* |
324
|
|
|
* @return bool |
325
|
|
|
*/ |
326
|
18 |
|
private function isInvalidLogDate($file) |
327
|
|
|
{ |
328
|
18 |
|
$pattern = '/laravel-(\d){4}-(\d){2}-(\d){2}.log/'; |
329
|
|
|
|
330
|
18 |
|
if ((bool) preg_match($pattern, $file, $matches) === false) { |
331
|
18 |
|
return true; |
332
|
|
|
} |
333
|
|
|
|
334
|
18 |
|
return false; |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|