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) |
|
50 | { |
||
51 | 23 | parent::__construct($config); |
|
52 | |||
53 | try { |
||
54 | 23 | $this->initLogger(); |
|
55 | 1 | } catch (\Exception $e) { |
|
56 | 1 | throw $e; |
|
57 | } |
||
58 | 22 | } |
|
59 | |||
60 | /** |
||
61 | * Init the logger. |
||
62 | */ |
||
63 | 23 | private function initLogger() |
|
64 | { |
||
65 | 23 | $monologConfig = $this->getMonologConfig(); |
|
66 | |||
67 | 23 | if (empty($monologConfig)) { |
|
68 | 1 | throw new BotonomousException('Monolog config is missing'); |
|
69 | } |
||
70 | |||
71 | 22 | $logger = new Logger($monologConfig['channel']); |
|
72 | |||
73 | 22 | foreach (array_keys($monologConfig[self::HANDLERS_KEY]) as $value) { |
|
74 | 22 | $logger = $this->pushMonologHandler($logger, $value); |
|
75 | } |
||
76 | |||
77 | 22 | $this->setLogger($logger); |
|
78 | 22 | } |
|
79 | |||
80 | /** |
||
81 | * @return mixed |
||
82 | */ |
||
83 | 23 | private function getMonologConfig() |
|
84 | { |
||
85 | 23 | $loggerConfig = $this->getConfig()->get('logger'); |
|
86 | |||
87 | 23 | return !empty($loggerConfig['monolog']) ? $loggerConfig['monolog'] : false; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param Logger $logger |
||
92 | * @param $handlerKey |
||
93 | * |
||
94 | * @return Logger |
||
95 | */ |
||
96 | 22 | private function pushMonologHandler(Logger $logger, string $handlerKey): Logger |
|
97 | { |
||
98 | 22 | $activeHandlers = []; |
|
99 | |||
100 | // if there are more $handlerKey, use switch |
||
101 | 22 | if ($handlerKey === 'file') { |
|
102 | 22 | $activeHandlers[] = new StreamHandler($this->getLogFilePath()); |
|
|
|||
103 | } |
||
104 | |||
105 | 22 | if (!empty($activeHandlers)) { |
|
106 | 22 | foreach ($activeHandlers as $activeHandler) { |
|
107 | 22 | $logger->pushHandler($activeHandler); |
|
108 | } |
||
109 | } |
||
110 | |||
111 | 22 | return $logger; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * @return false|string |
||
116 | */ |
||
117 | 22 | public function getLogFilePath() |
|
118 | { |
||
119 | 22 | $monologConfigFile = $this->getMonologConfigFileName(); |
|
120 | 22 | if (empty($monologConfigFile)) { |
|
121 | return false; |
||
122 | } |
||
123 | |||
124 | 22 | return $this->getTempDir().DIRECTORY_SEPARATOR.$monologConfigFile; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * @return mixed |
||
129 | */ |
||
130 | 22 | private function getMonologConfigFileName() |
|
131 | { |
||
132 | 22 | $monologConfig = $this->getMonologConfig(); |
|
133 | |||
134 | 22 | if (isset($monologConfig[self::HANDLERS_KEY]['file']['fileName'])) { |
|
135 | 22 | return $monologConfig[self::HANDLERS_KEY]['file']['fileName']; |
|
136 | } |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @return LoggerInterface |
||
141 | */ |
||
142 | 19 | private function getLogger(): LoggerInterface |
|
146 | |||
147 | /** |
||
148 | * @param LoggerInterface $logger |
||
149 | */ |
||
150 | 22 | private function setLogger(LoggerInterface $logger) |
|
154 | |||
155 | /** |
||
156 | * @param string $function |
||
157 | * @param string $message |
||
158 | * @param string $channel |
||
159 | * |
||
160 | * @throws \Exception |
||
161 | * |
||
162 | * @return bool |
||
163 | */ |
||
164 | 11 | public function logChat(string $function, string $message = '', string $channel = ''): bool |
|
176 | |||
177 | /** |
||
178 | * @throws \Exception |
||
179 | * |
||
180 | * @return bool |
||
181 | */ |
||
182 | 21 | private function canLog(): bool |
|
188 | |||
189 | /** |
||
190 | * @throws \Exception |
||
191 | * |
||
192 | * @return string |
||
193 | */ |
||
194 | 22 | public function getTempDir(): string |
|
198 | |||
199 | /** |
||
200 | * @param $function |
||
201 | * @param string $message |
||
202 | * @param $channel |
||
203 | * |
||
204 | * @return string |
||
205 | */ |
||
206 | 1 | public function getLogContent(string $function, string $message, string $channel): string |
|
210 | |||
211 | /** |
||
212 | * @param $message |
||
213 | * @param array $context |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | 1 | public function logDebug(string $message, array $context = []): bool |
|
221 | |||
222 | /** |
||
223 | * @param string $message |
||
224 | * @param array $context |
||
225 | * |
||
226 | * @return bool |
||
227 | */ |
||
228 | 13 | public function logInfo(string $message, array $context = []): bool |
|
232 | |||
233 | /** |
||
234 | * @param $message |
||
235 | * @param array $context |
||
236 | * |
||
237 | * @return bool |
||
238 | */ |
||
239 | 1 | public function logNotice(string $message, array $context = []): bool |
|
243 | |||
244 | /** |
||
245 | * @param $message |
||
246 | * @param array $context |
||
247 | * |
||
248 | * @return bool |
||
249 | */ |
||
250 | 1 | public function logWarning(string $message, array $context = []): bool |
|
254 | |||
255 | /** |
||
256 | * @param $message |
||
257 | * @param array $context |
||
258 | * |
||
259 | * @return bool |
||
260 | */ |
||
261 | 1 | public function logError(string $message, array $context = []): bool |
|
265 | |||
266 | /** |
||
267 | * @param $message |
||
268 | * @param array $context |
||
269 | * |
||
270 | * @return bool |
||
271 | */ |
||
272 | 1 | public function logCritical(string $message, array $context = []): bool |
|
276 | |||
277 | /** |
||
278 | * @param $message |
||
279 | * @param array $context |
||
280 | * |
||
281 | * @return bool |
||
282 | */ |
||
283 | 1 | public function logAlert(string $message, array $context = []): bool |
|
287 | |||
288 | /** |
||
289 | * @param $message |
||
290 | * @param array $context |
||
291 | * |
||
292 | * @return bool |
||
293 | */ |
||
294 | 1 | public function logEmergency(string $message, array $context = []): bool |
|
298 | |||
299 | /** |
||
300 | * @param string $level |
||
301 | * @param $message |
||
302 | * @param array $context |
||
303 | * |
||
304 | * @throws \Exception |
||
305 | * |
||
306 | * @return bool |
||
307 | */ |
||
308 | 21 | public function log(string $level, string $message, array $context = []): bool |
|
324 | } |
||
325 |