|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE |
|
6
|
|
|
* @link https://github.com/flipboxfactory/craft-ember |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\ember\controllers; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\helpers\DateTimeHelper; |
|
13
|
|
|
use yii\data\ArrayDataProvider; |
|
14
|
|
|
use yii\data\DataProviderInterface; |
|
15
|
|
|
use yii\data\Pagination; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author Flipbox Factory <[email protected]> |
|
19
|
|
|
* @since 1.0.8 |
|
20
|
|
|
*/ |
|
21
|
|
|
trait LogViewerTrait |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected abstract function logFile(): string; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function lineRegex(): string |
|
32
|
|
|
{ |
|
33
|
|
|
return '/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) \[(.*)\]\[(.*)\]\[(.*)\]\[(.*)\]\[(.*)\] (.*)/'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array $matches |
|
38
|
|
|
* @param array $log |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function lineMatcher(array $matches, array $log): array |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
return [ |
|
44
|
|
|
'time' => DateTimeHelper::toDateTime($matches[1]), |
|
45
|
|
|
'message' => $matches[7], |
|
46
|
|
|
'ip' => $matches[2], |
|
47
|
|
|
'userId' => $matches[3], |
|
48
|
|
|
'sessionId' => $matches[4], |
|
49
|
|
|
'level' => $matches[5], |
|
50
|
|
|
'category' => $matches[6], |
|
51
|
|
|
'vars' => '' |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $line |
|
57
|
|
|
* @param array $log |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function lineExtra(string $line, array $log): array |
|
61
|
|
|
{ |
|
62
|
|
|
$log['vars'] .= utf8_encode($line); |
|
63
|
|
|
|
|
64
|
|
|
return $log; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return DataProviderInterface |
|
69
|
|
|
* @throws \Exception |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getLogItems(): DataProviderInterface |
|
72
|
|
|
{ |
|
73
|
|
|
$file = Craft::getAlias($this->logFile()); |
|
74
|
|
|
|
|
75
|
|
|
if (!is_file($file)) { |
|
76
|
|
|
throw new \Exception("'$file' is not found"); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return new ArrayDataProvider([ |
|
80
|
|
|
'allModels' => $this->parseFile($file), |
|
81
|
|
|
'sort' => [ |
|
82
|
|
|
'attributes' => [ |
|
83
|
|
|
'time' => ['default' => SORT_DESC], |
|
84
|
|
|
'level' => ['default' => SORT_DESC] |
|
85
|
|
|
], |
|
86
|
|
|
], |
|
87
|
|
|
'pagination' => [ |
|
88
|
|
|
'class' => Pagination::class, |
|
89
|
|
|
'pageSizeParam' => 'size', |
|
90
|
|
|
'pageParam' => 'page', |
|
91
|
|
|
'pageSizeLimit' => 'limit', |
|
92
|
|
|
'defaultPageSize' => 200, |
|
93
|
|
|
] |
|
94
|
|
|
]); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function parseFile($file) |
|
101
|
|
|
{ |
|
102
|
|
|
$lines = []; |
|
103
|
|
|
$log = []; |
|
104
|
|
|
if ($file = fopen($file, "r")) { |
|
105
|
|
|
while (($line = fgets($file)) !== false) { |
|
106
|
|
|
// Log line |
|
107
|
|
|
if (preg_match($this->lineRegex(), $line, $matches)) { |
|
108
|
|
|
if (!empty($log)) { |
|
109
|
|
|
$lines[] = $log; |
|
110
|
|
|
} |
|
111
|
|
|
$log = $this->lineMatcher($matches, $log); |
|
112
|
|
|
} else { |
|
113
|
|
|
$log = $this->lineExtra($line, $log); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
fclose($file); |
|
117
|
|
|
} |
|
118
|
|
|
return $lines; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.