forked from krissss/yii2-log-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.php
202 lines (180 loc) · 4.43 KB
/
Log.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace kriss\logReader;
use Yii;
use yii\base\BaseObject;
use yii\caching\FileDependency;
use yii\helpers\FileHelper;
use yii\helpers\Inflector;
/**
* Class Log
*
* @property string $name
* @property string $alias
* @property null|string $stamp
* @property string $slug
* @property string $fileName
* @property boolean $isExist
* @property integer|null $size
* @property integer|null $updatedAt
* @property string $downloadName
* @property array $counts
*/
class Log extends BaseObject
{
private $_name;
private $_alias;
private $_stamp;
private $_fileName;
/**
* @param string $name
* @param string $alias
* @param null|string $stamp
* @param array $config
*/
public function __construct($name, $alias, $stamp = null, $config = [])
{
$this->_name = $name;
$this->_alias = $alias;
$this->_stamp = $stamp;
$this->_fileName = static::extractFileName($alias, $stamp);
parent::__construct($config);
}
/**
* @return string
*/
public function getName()
{
return $this->_name;
}
/**
* @return string
*/
public function getAlias()
{
return $this->_alias;
}
/**
* @return string
*/
public function getStamp()
{
return $this->_stamp;
}
/**
* @return string
*/
public function getSlug()
{
return static::extractSlug($this->_name);
}
/**
* @return string
*/
public function getFileName()
{
return $this->_fileName;
}
/**
* @return boolean
*/
public function getIsExist()
{
return file_exists($this->getFileName());
}
/**
* @return integer|null
*/
public function getSize()
{
return $this->getIsExist() ? filesize($this->getFileName()) : null;
}
/**
* @return integer|null
*/
public function getUpdatedAt()
{
return $this->getIsExist() ? filemtime($this->getFileName()) : null;
}
/**
* @return string
*/
public function getDownloadName()
{
return $this->getSlug() . '.log';
}
/**
* @param bool $force
* @return array
*/
public function getCounts($force = false)
{
if (!$this->getIsExist()) return [];
$key = $this->getFileName() . '#counts';
if (!$force && ($counts = Yii::$app->cache->get($key)) !== false) {
return $counts;
}
$counts = [];
if ($h = fopen($this->getFileName(), 'r')) {
while (($line = fgets($h)) !== false) {
if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $line)) {
if (preg_match('/^[\d\-\: ]+\[.*\]\[.*\]\[.*\]\[(.*)\]/U', $line, $m)) {
$level = $m[1];
if (!isset($counts[$level])) $counts[$level] = 0;
$counts[$level]++;
}
}
}
fclose($h);
Yii::$app->cache->set($key, $counts, 0, new FileDependency([
'fileName' => $this->getFileName(),
]));
}
return $counts;
}
/**
* @param string $stamp
* @return boolean
*/
public function archive($stamp)
{
if ($this->getStamp() === null && $this->getIsExist()) {
rename($this->getFileName(), static::extractFileName($this->getAlias(), $stamp));
return true;
} else {
return false;
}
}
/**
* @param string $name
* @return string
*/
public static function extractSlug($name)
{
return Inflector::slug($name);
}
/**
* @param string $alias
* @param null|string $stamp
* @return string
*/
public static function extractFileName($alias, $stamp = null)
{
$fileName = FileHelper::normalizePath(Yii::getAlias($alias, false));
if ($stamp === null) return $fileName;
return $fileName . '.' . $stamp;
}
/**
* @param string $alias
* @param string $fileName
* @return string|null
*/
public static function extractFileStamp($alias, $fileName)
{
$originName = FileHelper::normalizePath(Yii::getAlias($alias, false));
if (strpos($fileName, $originName) === 0) {
return substr($fileName, strlen($originName) + 1);
} else {
return null;
}
}
}