1 | <?php namespace Arcanedev\LogViewer\Entities; |
||
14 | class LogEntry implements Arrayable, Jsonable, JsonSerializable |
||
15 | { |
||
16 | /* ------------------------------------------------------------------------------------------------ |
||
17 | | Properties |
||
18 | | ------------------------------------------------------------------------------------------------ |
||
19 | */ |
||
20 | /** @var string */ |
||
21 | public $env; |
||
22 | |||
23 | /** @var string */ |
||
24 | public $level; |
||
25 | |||
26 | /** @var Carbon */ |
||
27 | public $datetime; |
||
28 | |||
29 | /** @var string */ |
||
30 | public $header; |
||
31 | |||
32 | /** @var string */ |
||
33 | public $stack; |
||
34 | |||
35 | /* ------------------------------------------------------------------------------------------------ |
||
36 | | Constructor |
||
37 | | ------------------------------------------------------------------------------------------------ |
||
38 | */ |
||
39 | /** |
||
40 | * Construct the log entry instance. |
||
41 | * |
||
42 | * @param string $level |
||
43 | * @param string $header |
||
44 | * @param string $stack |
||
45 | */ |
||
46 | 864 | public function __construct($level, $header, $stack) |
|
47 | { |
||
48 | 864 | $this->setLevel($level); |
|
49 | 864 | $this->setHeader($header); |
|
50 | 864 | $this->setStack($stack); |
|
51 | 864 | } |
|
52 | |||
53 | /* ------------------------------------------------------------------------------------------------ |
||
54 | | Getters & Setters |
||
55 | | ------------------------------------------------------------------------------------------------ |
||
56 | */ |
||
57 | /** |
||
58 | * Set the entry level. |
||
59 | * |
||
60 | * @param string $level |
||
61 | * |
||
62 | * @return self |
||
63 | */ |
||
64 | 864 | private function setLevel($level) |
|
65 | { |
||
66 | 864 | $this->level = $level; |
|
67 | |||
68 | 864 | return $this; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Set the entry header. |
||
73 | * |
||
74 | * @param string $header |
||
75 | * |
||
76 | * @return self |
||
77 | */ |
||
78 | 864 | private function setHeader($header) |
|
79 | { |
||
80 | 864 | $this->setDatetime($this->extractDatetime($header)); |
|
81 | |||
82 | 864 | $header = $this->cleanHeader($header); |
|
83 | |||
84 | 864 | if (preg_match('/^[a-z]+.[A-Z]+:/', $header, $out)) { |
|
85 | 864 | $this->setEnv($out[0]); |
|
86 | 864 | $header = trim(str_replace($out[0], '', $header)); |
|
87 | 648 | } |
|
88 | |||
89 | 864 | $this->header = $header; |
|
90 | |||
91 | 864 | return $this; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Set entry environment. |
||
96 | * |
||
97 | * @param string $env |
||
98 | * |
||
99 | * @return self |
||
100 | */ |
||
101 | 864 | private function setEnv($env) |
|
102 | { |
||
103 | 864 | $this->env = head(explode('.', $env)); |
|
104 | |||
105 | 864 | return $this; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Set the entry date time. |
||
110 | * |
||
111 | * @param string $datetime |
||
112 | * |
||
113 | * @return self |
||
114 | */ |
||
115 | 864 | private function setDatetime($datetime) |
|
116 | { |
||
117 | 864 | $this->datetime = Carbon::createFromFormat( |
|
118 | 864 | 'Y-m-d H:i:s', |
|
119 | $datetime |
||
120 | 648 | ); |
|
121 | |||
122 | 864 | return $this; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Set the entry stack. |
||
127 | * |
||
128 | * @param string $stack |
||
129 | * |
||
130 | * @return self |
||
131 | */ |
||
132 | 864 | private function setStack($stack) |
|
133 | { |
||
134 | 864 | $this->stack = $stack; |
|
135 | |||
136 | 864 | return $this; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * Get translated level name with icon |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | 24 | public function level() |
|
145 | { |
||
146 | 24 | return $this->icon() . ' ' . $this->name(); |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * Get translated level name |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | 24 | public function name() |
|
155 | { |
||
156 | 24 | return trans('log-viewer::levels.' . $this->level); |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get level icon |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | 24 | public function icon() |
|
165 | { |
||
166 | 24 | return log_styler()->icon($this->level); |
|
167 | } |
||
168 | |||
169 | /* ------------------------------------------------------------------------------------------------ |
||
170 | | Check Functions |
||
171 | | ------------------------------------------------------------------------------------------------ |
||
172 | */ |
||
173 | /** |
||
174 | * Check if same log level |
||
175 | * |
||
176 | * @param string $level |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | 96 | public function isSameLevel($level) |
|
184 | |||
185 | /* ------------------------------------------------------------------------------------------------ |
||
186 | | Convert Functions |
||
187 | | ------------------------------------------------------------------------------------------------ |
||
188 | */ |
||
189 | /** |
||
190 | * Get the log entry as an array. |
||
191 | * |
||
192 | * @return array |
||
193 | */ |
||
194 | 24 | public function toArray() |
|
195 | { |
||
196 | return [ |
||
197 | 24 | 'level' => $this->level, |
|
198 | 24 | 'datetime' => $this->datetime->format('Y-m-d H:i:s'), |
|
199 | 24 | 'header' => $this->header, |
|
200 | 24 | 'stack' => $this->stack |
|
201 | 18 | ]; |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * Convert the log entry to its JSON representation. |
||
206 | * |
||
207 | * @param int $options |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | 12 | public function toJson($options = 0) |
|
215 | |||
216 | /** |
||
217 | * Serialize the log entry object to json data |
||
218 | * |
||
219 | * @return array |
||
220 | */ |
||
221 | 12 | public function jsonSerialize() |
|
225 | |||
226 | /* ------------------------------------------------------------------------------------------------ |
||
227 | | Check Functions |
||
228 | | ------------------------------------------------------------------------------------------------ |
||
229 | */ |
||
230 | /** |
||
231 | * Check if the entry has a stack. |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | 24 | public function hasStack() |
|
239 | |||
240 | /* ------------------------------------------------------------------------------------------------ |
||
241 | | Other Functions |
||
242 | | ------------------------------------------------------------------------------------------------ |
||
243 | */ |
||
244 | /** |
||
245 | * Clean the entry header. |
||
246 | * |
||
247 | * @param string $header |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | 864 | private function cleanHeader($header) |
|
255 | |||
256 | /** |
||
257 | * Extract datetime from the header. |
||
258 | * |
||
259 | * @param string $header |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | private function extractDatetime($header) |
||
264 | { |
||
265 | return preg_replace('/^\[(' . REGEX_DATETIME_PATTERN . ')\].*/', '$1', $header); |
||
267 | } |
||
268 |