-
Notifications
You must be signed in to change notification settings - Fork 9
/
log.php
262 lines (230 loc) · 6.69 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/**
* A class designed to take care of showing the log file.
*
* @author Lauge Rud Knudsen <laugerudknudsen@gmail.com>
* @version V2
*/
class Log {
protected $log;
protected $datetimeFilterFrom;
protected $datetimeFilterTo;
protected $levelFilter = [];
//constants
const UNKNOWN_DATETIME_MESSAGE = 'Unknown Date and time';
const UNKNOWN_LEVEL_MESSAGE = 'Unknown error level';
const EMPTY_ERROR_MESSAGE = 'Message was empty';
const EMPTY_FILE_MESSAGE = 'File was empty';
const EMPTY_LINE_MESSAGE = 'Line was empty';
const DATETIME_FORMAT = 'Y-m-d H:i:s';
//getters
/**
* Get all filtered levels.
*
* @return string[] An array containing all filtered levels.
*/
public function getLevels() {
return $this->levelFilter;
}
/**
* Get the datetimeFrom filter value.
*
* @return datetime The date and time.
*/
public function getDatetimeFrom() {
return $this->datetimeFilterFrom;
}
/**
* Get the datetimeTo filter value.
*
* @return datetime The date and time.
*/
public function getDatetimeTo() {
return $this->datetimeFilterTo;
}
/**
* Get all valid level values.
*
* @return string[] An array containing all possible level values.
*/
public function getAllLevels() {
return ['notice', 'warning', 'error'];
}
//setters
/**
* Sets the level filters.
*
* @param string[] $levels The levels that should be filtered.
*/
public function setFilterLevels($levels = []) {
$output = [];
foreach ($this->getAllLevels() as $level) {
if (array_search($level, $levels) !== FALSE) {
$output[] = $level;
}
}
$this->levelFilter = $output;
}
/**
* Sets the date from filter.
*
* @param string $date The date and time as a string.
*/
public function setFilterDateFrom($date) {
$this->datetimeFilterFrom = DateTime::createFromFormat(self::DATETIME_FORMAT, $date);
}
/**
* Sets the date to filter.
*
* @param string $date The date and time as a string.
*/
public function setFilterDateTo($date) {
$this->datetimeFilterTo = DateTime::createFromFormat(self::DATETIME_FORMAT, $date);
}
//public functions
function __construct() {
if (file_exists(FISHY_LOG_PATH)) {
$this->log = file(FISHY_LOG_PATH);
}
}
/**
* Reads in the log and returns it.
*
* @return array[]|false An array containing each row or false if the log is invalid.
*/
public function read() {
if (!empty($this->log)) {
if (empty($this->datetimeFilterFrom)) {
$this->resetDateFromFilter();
}
if (empty($this->datetimeFilterTo)) {
$this->setFilterDateTo(date(self::DATETIME_FORMAT));
}
foreach ($this->log as $line) {
$row = $this->toRow($line);
if (!empty($row)) {
$row['color'] = $this->getColor($line);
$output[] = $row;
}
}
}
if (!isset($output)) {
$output = false;
}
return $output;
}
/**
* Check if a desired level is filtered.
*
* @param string $level The level to be searched.
* @return bool Whether or not the level is filtered.
*/
public function hasLevelFilter($level) {
return (array_search($level, $this->levelFilter) !== FALSE);
}
/**
* Sets the dateFromFilter to the unix epoch.
*/
public function resetDateFromFilter() {
$this->datetimeFilterFrom = (new DateTime())->setTimestamp(0);
}
//private functions
/**
* Gets the color of a specified line.
*
* @param string $line The line in question as a string.
* @return string|false A string containing the hex code for the error level.
*/
protected function getColor($line) {
$output = false;
switch ($this->getLevel($line)) {
case 'notice':
$output = '#0000ff';
break;
case 'warning':
$output = '#ffff00';
break;
case 'error':
$output = '#ff0000';
break;
default:
$output = '#ffffff';
break;
}
return $output;
}
/**
* Convert the specified line to a table row.
*
* @param string $line The line in question as a string.
*/
protected function toRow($line) {
$level = $this->getLevel($line);
if ($this->hasLevelFilter($level)) {
return null;
}
$datetimeStart = strpos($line, '[');
$datetimeEnd = strpos($line, ']');
if ($datetimeStart === 0 && $datetimeEnd > 0) {
$datetime = DateTime::createFromFormat(self::DATETIME_FORMAT, substr($line, $datetimeStart + 1, $datetimeEnd - 1));
}
if (isset($datetime) < $this->datetimeFilterFrom || $datetime > $this->datetimeFilterTo) {
return null;
}
$messageStart = strpos($line, ':', $datetimeEnd);
$fileStart = strpos($line, '[at] ', $messageStart);
if ($fileStart === FALSE) {
$message = substr($line, $messageStart + 2);
} else {
$message = substr($line, $messageStart + 2, $fileStart - $datetimeEnd - 7);
}
$lineStart = strpos($line, '[on line] ');
if ($fileStart === FALSE) {
$file = '';
} else {
$file = substr($line, $fileStart + 5, $lineStart - $fileStart - 6);
}
if ($lineStart === FALSE) {
$lineNum = '';
} else {
$lineNum = substr($line, $lineStart + 10);
}
$output['datetime'] = (isset($datetime)) ? addslashes($datetime->format(self::DATETIME_FORMAT)) : self::UNKNOWN_DATETIME_MESSAGE;
$output['level'] = ($level !== FALSE) ? addslashes($level) : self::UNKNOWN_LEVEL_MESSAGE;
$output['message'] = (isset($message)) ? addslashes($message) : self::EMPTY_ERROR_MESSAGE;
$output['file'] = (isset($file)) ? addslashes($file) : self::EMPTY_FILE_MESSAGE;
$output['line'] = (isset($lineNum)) ? addslashes($lineNum) : self::EMPTY_LINE_MESSAGE;
return $output;
}
/**
* Get the level of the specified line.
*
* @param string $line The line in question as a string.
* @return string|false Returns the string name of the level, false if it wasn't found.
*/
protected function getLevel($line) {
$output = false;
foreach ($this->getAllLevels() as $level) {
$pos = strpos($line, "] $level:");
if ($pos !== FALSE) {
$output = $level;
}
}
/*
$log = strpos($line, '] log:');
$notice = strpos($line, '] notice:');
$warning = strpos($line, '] warning:');
$error = strpos($line, '] error:');
if ($log !== FALSE) {
$output = 'log';
} else if ($notice !== FALSE) {
$output = 'notice';
} else if ($warning !== FALSE) {
$output = 'warning';
} else if ($error !== FALSE) {
$output = 'error';
}
*/
return $output;
}
}