1 | <?php |
||
13 | class LoggerUtility extends AbstractUtility |
||
14 | { |
||
15 | const DATE_FORMAT = 'Y-m-d H:i:s'; |
||
16 | const TEMP_FOLDER = 'tmp'; |
||
17 | |||
18 | const DEBUG = 'debug'; |
||
19 | const INFO = 'info'; |
||
20 | const NOTICE = 'notice'; |
||
21 | const WARNING = 'warning'; |
||
22 | const ERROR = 'error'; |
||
23 | const CRITICAL = 'critical'; |
||
24 | const ALERT = 'alert'; |
||
25 | const EMERGENCY = 'emergency'; |
||
26 | |||
27 | const HANDLERS_KEY = 'handlers'; |
||
28 | |||
29 | public static $levels = [ |
||
30 | self::DEBUG, |
||
31 | self::INFO, |
||
32 | self::NOTICE, |
||
33 | self::WARNING, |
||
34 | self::ERROR, |
||
35 | self::CRITICAL, |
||
36 | self::ALERT, |
||
37 | self::EMERGENCY, |
||
38 | ]; |
||
39 | |||
40 | private $logger; |
||
41 | |||
42 | /** |
||
43 | * LoggerUtility constructor. |
||
44 | * |
||
45 | * @param null $config |
||
46 | * |
||
47 | * @throws \Exception |
||
48 | */ |
||
49 | 23 | public function __construct($config = null) |
|
59 | |||
60 | /** |
||
61 | * Init the logger. |
||
62 | * |
||
63 | * @throws \Exception |
||
64 | */ |
||
65 | 23 | private function initLogger() |
|
81 | |||
82 | /** |
||
83 | * @throws \Exception |
||
84 | * |
||
85 | * @return mixed |
||
86 | */ |
||
87 | 23 | private function getMonologConfig() |
|
93 | |||
94 | /** |
||
95 | * @param Logger $logger |
||
96 | * @param string $handlerKey |
||
97 | * |
||
98 | * @throws \Exception |
||
99 | * |
||
100 | * @return Logger |
||
101 | */ |
||
102 | 22 | private function pushMonologHandler(Logger $logger, string $handlerKey): Logger |
|
119 | |||
120 | /** |
||
121 | * @throws \Exception |
||
122 | * |
||
123 | * @return false|string |
||
124 | */ |
||
125 | 22 | public function getLogFilePath() |
|
134 | |||
135 | /** |
||
136 | * @throws \Exception |
||
137 | * |
||
138 | * @return mixed |
||
139 | */ |
||
140 | 22 | private function getMonologConfigFileName() |
|
148 | |||
149 | /** |
||
150 | * @return LoggerInterface |
||
151 | */ |
||
152 | 19 | private function getLogger(): LoggerInterface |
|
156 | |||
157 | /** |
||
158 | * @param LoggerInterface $logger |
||
159 | */ |
||
160 | 22 | private function setLogger(LoggerInterface $logger) |
|
164 | |||
165 | /** |
||
166 | * @param string $function |
||
167 | * @param string $message |
||
168 | * @param string $channel |
||
169 | * |
||
170 | * @throws \Exception |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | 11 | public function logChat(string $function, string $message = '', string $channel = ''): bool |
|
186 | |||
187 | /** |
||
188 | * @throws \Exception |
||
189 | * |
||
190 | * @return bool |
||
191 | */ |
||
192 | 21 | private function canLog(): bool |
|
198 | |||
199 | /** |
||
200 | * @throws \Exception |
||
201 | * |
||
202 | * @return string |
||
203 | */ |
||
204 | 22 | public function getTempDir(): string |
|
208 | |||
209 | /** |
||
210 | * @param $function |
||
211 | * @param string $message |
||
212 | * @param $channel |
||
213 | * |
||
214 | * @return string |
||
215 | */ |
||
216 | 1 | public function getLogContent(string $function, string $message, string $channel): string |
|
220 | |||
221 | /** |
||
222 | * @param string $message |
||
223 | * @param array $context |
||
224 | * |
||
225 | * @throws \Exception |
||
226 | * |
||
227 | * @return bool |
||
228 | */ |
||
229 | 1 | public function logDebug(string $message, array $context = []): bool |
|
233 | |||
234 | /** |
||
235 | * @param string $message |
||
236 | * @param array $context |
||
237 | * |
||
238 | * @throws \Exception |
||
239 | * |
||
240 | * @return bool |
||
241 | */ |
||
242 | 13 | public function logInfo(string $message, array $context = []): bool |
|
246 | |||
247 | /** |
||
248 | * @param string $message |
||
249 | * @param array $context |
||
250 | * |
||
251 | * @throws \Exception |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | 1 | public function logNotice(string $message, array $context = []): bool |
|
259 | |||
260 | /** |
||
261 | * @param string $message |
||
262 | * @param array $context |
||
263 | * |
||
264 | * @throws \Exception |
||
265 | * |
||
266 | * @return bool |
||
267 | */ |
||
268 | 1 | public function logWarning(string $message, array $context = []): bool |
|
272 | |||
273 | /** |
||
274 | * @param string $message |
||
275 | * @param array $context |
||
276 | * |
||
277 | * @throws \Exception |
||
278 | * |
||
279 | * @return bool |
||
280 | */ |
||
281 | 1 | public function logError(string $message, array $context = []): bool |
|
285 | |||
286 | /** |
||
287 | * @param string $message |
||
288 | * @param array $context |
||
289 | * |
||
290 | * @throws \Exception |
||
291 | * |
||
292 | * @return bool |
||
293 | */ |
||
294 | 1 | public function logCritical(string $message, array $context = []): bool |
|
298 | |||
299 | /** |
||
300 | * @param string $message |
||
301 | * @param array $context |
||
302 | * |
||
303 | * @throws \Exception |
||
304 | * |
||
305 | * @return bool |
||
306 | */ |
||
307 | 1 | public function logAlert(string $message, array $context = []): bool |
|
311 | |||
312 | /** |
||
313 | * @param string $message |
||
314 | * @param array $context |
||
315 | * |
||
316 | * @throws \Exception |
||
317 | * |
||
318 | * @return bool |
||
319 | */ |
||
320 | 1 | public function logEmergency(string $message, array $context = []): bool |
|
324 | |||
325 | /** |
||
326 | * @param string $level |
||
327 | * @param $message |
||
328 | * @param array $context |
||
329 | * |
||
330 | * @throws \Exception |
||
331 | * |
||
332 | * @return bool |
||
333 | */ |
||
334 | 21 | public function log(string $level, string $message, array $context = []): bool |
|
350 | } |
||
351 |