|
1
|
|
|
<?php namespace Arcanedev\LogViewer\Entities; |
|
2
|
|
|
|
|
3
|
|
|
use Carbon\Carbon; |
|
4
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
5
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
|
6
|
|
|
use JsonSerializable; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class LogEntry |
|
10
|
|
|
* |
|
11
|
|
|
* @package Arcanedev\LogViewer\Entities |
|
12
|
|
|
* @author ARCANEDEV <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class LogEntry implements Arrayable, Jsonable, JsonSerializable |
|
15
|
|
|
{ |
|
16
|
|
|
/* ----------------------------------------------------------------- |
|
17
|
|
|
| Properties |
|
18
|
|
|
| ----------------------------------------------------------------- |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
public $env; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
public $level; |
|
26
|
|
|
|
|
27
|
|
|
/** @var \Carbon\Carbon */ |
|
28
|
|
|
public $datetime; |
|
29
|
|
|
|
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
public $header; |
|
32
|
|
|
|
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
public $stack; |
|
35
|
|
|
|
|
36
|
|
|
/** @var array */ |
|
37
|
|
|
public $context = []; |
|
38
|
|
|
|
|
39
|
|
|
/* ----------------------------------------------------------------- |
|
40
|
|
|
| Constructor |
|
41
|
|
|
| ----------------------------------------------------------------- |
|
42
|
|
|
*/ |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Construct the log entry instance. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $level |
|
48
|
|
|
* @param string $header |
|
49
|
|
|
* @param string|null $stack |
|
50
|
|
|
*/ |
|
51
|
189 |
|
public function __construct($level, $header, $stack = null) |
|
52
|
|
|
{ |
|
53
|
189 |
|
$this->setLevel($level); |
|
54
|
189 |
|
$this->setHeader($header); |
|
55
|
189 |
|
$this->setStack($stack); |
|
56
|
189 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/* ----------------------------------------------------------------- |
|
59
|
|
|
| Getters & Setters |
|
60
|
|
|
| ----------------------------------------------------------------- |
|
61
|
|
|
*/ |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Set the entry level. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $level |
|
67
|
|
|
* |
|
68
|
|
|
* @return self |
|
69
|
|
|
*/ |
|
70
|
189 |
|
private function setLevel($level) |
|
71
|
|
|
{ |
|
72
|
189 |
|
$this->level = $level; |
|
73
|
|
|
|
|
74
|
189 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Set the entry header. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $header |
|
81
|
|
|
* |
|
82
|
|
|
* @return self |
|
83
|
|
|
*/ |
|
84
|
189 |
|
private function setHeader($header) |
|
85
|
|
|
{ |
|
86
|
189 |
|
$this->setDatetime($this->extractDatetime($header)); |
|
87
|
|
|
|
|
88
|
189 |
|
$header = $this->cleanHeader($header); |
|
89
|
|
|
|
|
90
|
189 |
|
$this->header = trim($header); |
|
91
|
|
|
|
|
92
|
189 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Set the context. |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $context |
|
99
|
|
|
* |
|
100
|
|
|
* @return $this |
|
101
|
|
|
*/ |
|
102
|
3 |
|
private function setContext(array $context) |
|
103
|
|
|
{ |
|
104
|
3 |
|
$this->context = $context; |
|
105
|
|
|
|
|
106
|
3 |
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Set entry environment. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $env |
|
113
|
|
|
* |
|
114
|
|
|
* @return self |
|
115
|
|
|
*/ |
|
116
|
189 |
|
private function setEnv($env) |
|
117
|
|
|
{ |
|
118
|
189 |
|
$this->env = head(explode('.', $env)); |
|
119
|
|
|
|
|
120
|
189 |
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Set the entry date time. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $datetime |
|
127
|
|
|
* |
|
128
|
|
|
* @return \Arcanedev\LogViewer\Entities\LogEntry |
|
129
|
|
|
*/ |
|
130
|
189 |
|
private function setDatetime($datetime) |
|
131
|
|
|
{ |
|
132
|
189 |
|
$this->datetime = Carbon::createFromFormat('Y-m-d H:i:s', $datetime); |
|
133
|
|
|
|
|
134
|
189 |
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Set the entry stack. |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $stack |
|
141
|
|
|
* |
|
142
|
|
|
* @return self |
|
143
|
|
|
*/ |
|
144
|
189 |
|
private function setStack($stack) |
|
145
|
|
|
{ |
|
146
|
189 |
|
$this->stack = $stack; |
|
147
|
|
|
|
|
148
|
189 |
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get translated level name with icon. |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
18 |
|
public function level() |
|
157
|
|
|
{ |
|
158
|
18 |
|
return $this->icon()->toHtml().' '.$this->name(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Get translated level name. |
|
163
|
|
|
* |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
18 |
|
public function name() |
|
167
|
|
|
{ |
|
168
|
18 |
|
return log_levels()->get($this->level); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Get level icon. |
|
173
|
|
|
* |
|
174
|
|
|
* @return \Illuminate\Support\HtmlString |
|
175
|
|
|
*/ |
|
176
|
18 |
|
public function icon() |
|
177
|
|
|
{ |
|
178
|
18 |
|
return log_styler()->icon($this->level); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get the entry stack. |
|
183
|
|
|
* |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
21 |
|
public function stack() |
|
187
|
|
|
{ |
|
188
|
21 |
|
return trim(htmlentities($this->stack)); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Get the entry context as json pretty print. |
|
193
|
|
|
* |
|
194
|
|
|
* @return string |
|
195
|
|
|
*/ |
|
196
|
3 |
|
public function context() |
|
197
|
|
|
{ |
|
198
|
3 |
|
return json_encode($this->context, JSON_PRETTY_PRINT); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/* ----------------------------------------------------------------- |
|
202
|
|
|
| Check Methods |
|
203
|
|
|
| ----------------------------------------------------------------- |
|
204
|
|
|
*/ |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Check if same log level. |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $level |
|
210
|
|
|
* |
|
211
|
|
|
* @return bool |
|
212
|
|
|
*/ |
|
213
|
27 |
|
public function isSameLevel($level) |
|
214
|
|
|
{ |
|
215
|
27 |
|
return $this->level === $level; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/* ----------------------------------------------------------------- |
|
219
|
|
|
| Convert Methods |
|
220
|
|
|
| ----------------------------------------------------------------- |
|
221
|
|
|
*/ |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Get the log entry as an array. |
|
225
|
|
|
* |
|
226
|
|
|
* @return array |
|
227
|
|
|
*/ |
|
228
|
6 |
|
public function toArray() |
|
229
|
|
|
{ |
|
230
|
|
|
return [ |
|
231
|
6 |
|
'level' => $this->level, |
|
232
|
6 |
|
'datetime' => $this->datetime->format('Y-m-d H:i:s'), |
|
233
|
6 |
|
'header' => $this->header, |
|
234
|
6 |
|
'stack' => $this->stack |
|
235
|
|
|
]; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Convert the log entry to its JSON representation. |
|
240
|
|
|
* |
|
241
|
|
|
* @param int $options |
|
242
|
|
|
* |
|
243
|
|
|
* @return string |
|
244
|
|
|
*/ |
|
245
|
3 |
|
public function toJson($options = 0) |
|
246
|
|
|
{ |
|
247
|
3 |
|
return json_encode($this->toArray(), $options); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Serialize the log entry object to json data. |
|
252
|
|
|
* |
|
253
|
|
|
* @return array |
|
254
|
|
|
*/ |
|
255
|
3 |
|
public function jsonSerialize() |
|
256
|
|
|
{ |
|
257
|
3 |
|
return $this->toArray(); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/* ----------------------------------------------------------------- |
|
261
|
|
|
| Check Methods |
|
262
|
|
|
| ----------------------------------------------------------------- |
|
263
|
|
|
*/ |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Check if the entry has a stack. |
|
267
|
|
|
* |
|
268
|
|
|
* @return bool |
|
269
|
|
|
*/ |
|
270
|
18 |
|
public function hasStack() |
|
271
|
|
|
{ |
|
272
|
18 |
|
return $this->stack !== "\n"; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Check if the entry has a context. |
|
277
|
|
|
* |
|
278
|
|
|
* @return bool |
|
279
|
|
|
*/ |
|
280
|
24 |
|
public function hasContext() |
|
281
|
|
|
{ |
|
282
|
24 |
|
return ! empty($this->context); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/* ----------------------------------------------------------------- |
|
286
|
|
|
| Other Methods |
|
287
|
|
|
| ----------------------------------------------------------------- |
|
288
|
|
|
*/ |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Clean the entry header. |
|
292
|
|
|
* |
|
293
|
|
|
* @param string $header |
|
294
|
|
|
* |
|
295
|
|
|
* @return string |
|
296
|
|
|
*/ |
|
297
|
189 |
|
private function cleanHeader($header) |
|
298
|
|
|
{ |
|
299
|
|
|
// REMOVE THE DATE |
|
300
|
189 |
|
$header = preg_replace('/\['.REGEX_DATETIME_PATTERN.'\][ ]/', '', $header); |
|
301
|
|
|
|
|
302
|
|
|
// EXTRACT ENV |
|
303
|
189 |
|
if (preg_match('/^[a-z]+.[A-Z]+:/', $header, $out)) { |
|
304
|
189 |
|
$this->setEnv($out[0]); |
|
305
|
189 |
|
$header = trim(str_replace($out[0], '', $header)); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
// EXTRACT CONTEXT (Regex from https://stackoverflow.com/a/21995025) |
|
309
|
189 |
|
preg_match_all('/{(?:[^{}]|(?R))*}/x', $header, $out); |
|
310
|
189 |
|
if (isset($out[0][0]) && ! is_null($context = json_decode($out[0][0], true))) { |
|
311
|
3 |
|
$header = str_replace($out[0][0], '', $header); |
|
312
|
3 |
|
$this->setContext($context); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
189 |
|
return $header; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Extract datetime from the header. |
|
320
|
|
|
* |
|
321
|
|
|
* @param string $header |
|
322
|
|
|
* |
|
323
|
|
|
* @return string |
|
324
|
|
|
*/ |
|
325
|
189 |
|
private function extractDatetime($header) |
|
326
|
|
|
{ |
|
327
|
189 |
|
return preg_replace('/^\[('.REGEX_DATETIME_PATTERN.')\].*/', '$1', $header); |
|
328
|
|
|
} |
|
329
|
|
|
} |
|
330
|
|
|
|