|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Botonomous\utility; |
|
4
|
|
|
|
|
5
|
|
|
use Monolog\Handler\StreamHandler; |
|
6
|
|
|
use Monolog\Logger; |
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class LoggerUtility. |
|
11
|
|
|
*/ |
|
12
|
|
|
class LoggerUtility extends AbstractUtility |
|
13
|
|
|
{ |
|
14
|
|
|
const DATE_FORMAT = 'Y-m-d H:i:s'; |
|
15
|
|
|
const TEMP_FOLDER = 'tmp'; |
|
16
|
|
|
|
|
17
|
|
|
const DEBUG = 'debug'; |
|
18
|
|
|
const INFO = 'info'; |
|
19
|
|
|
const NOTICE = 'notice'; |
|
20
|
|
|
const WARNING = 'warning'; |
|
21
|
|
|
const ERROR = 'error'; |
|
22
|
|
|
const CRITICAL = 'critical'; |
|
23
|
|
|
const ALERT = 'alert'; |
|
24
|
|
|
const EMERGENCY = 'emergency'; |
|
25
|
|
|
|
|
26
|
|
|
public static $levels = [ |
|
27
|
|
|
self::DEBUG, |
|
28
|
|
|
self::INFO, |
|
29
|
|
|
self::NOTICE, |
|
30
|
|
|
self::WARNING, |
|
31
|
|
|
self::ERROR, |
|
32
|
|
|
self::CRITICAL, |
|
33
|
|
|
self::ALERT, |
|
34
|
|
|
self::EMERGENCY, |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
private $logger; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* LoggerUtility constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param null $config |
|
43
|
|
|
* |
|
44
|
|
|
* @throws \Exception |
|
45
|
|
|
*/ |
|
46
|
23 |
|
public function __construct($config = null) |
|
47
|
|
|
{ |
|
48
|
23 |
|
parent::__construct($config); |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
23 |
|
$this->initLogger(); |
|
52
|
1 |
|
} catch (\Exception $e) { |
|
53
|
1 |
|
throw $e; |
|
54
|
|
|
} |
|
55
|
22 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Init the logger. |
|
59
|
|
|
*/ |
|
60
|
23 |
|
private function initLogger() |
|
61
|
|
|
{ |
|
62
|
23 |
|
$monologConfig = $this->getMonologConfig(); |
|
63
|
|
|
|
|
64
|
23 |
|
if (empty($monologConfig)) { |
|
65
|
1 |
|
throw new \Exception('Monolog config is missing'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
22 |
|
$logger = new Logger($monologConfig['channel']); |
|
69
|
|
|
|
|
70
|
22 |
|
foreach (array_keys($monologConfig['handlers']) as $value) { |
|
71
|
22 |
|
$logger = $this->pushMonologHandler($logger, $value); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
22 |
|
$this->setLogger($logger); |
|
75
|
22 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
23 |
|
private function getMonologConfig() |
|
81
|
|
|
{ |
|
82
|
23 |
|
$loggerConfig = $this->getConfig()->get('logger'); |
|
83
|
|
|
|
|
84
|
23 |
|
return !empty($loggerConfig['monolog']) ? $loggerConfig['monolog'] : false; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param Logger $logger |
|
89
|
|
|
* @param $handlerKey |
|
90
|
|
|
* |
|
91
|
|
|
* @return Logger |
|
92
|
|
|
*/ |
|
93
|
22 |
|
private function pushMonologHandler(Logger $logger, $handlerKey) |
|
94
|
|
|
{ |
|
95
|
22 |
|
$activeHandlers = []; |
|
96
|
|
|
|
|
97
|
|
|
switch ($handlerKey) { |
|
98
|
22 |
|
case 'file': |
|
99
|
22 |
|
$activeHandlers[] = new StreamHandler($this->getLogFilePath()); |
|
|
|
|
|
|
100
|
22 |
|
break; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
22 |
|
if (!empty($activeHandlers)) { |
|
104
|
22 |
|
foreach ($activeHandlers as $activeHandler) { |
|
105
|
22 |
|
$logger->pushHandler($activeHandler); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
22 |
|
return $logger; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return false|string |
|
114
|
|
|
*/ |
|
115
|
22 |
|
public function getLogFilePath() |
|
116
|
|
|
{ |
|
117
|
22 |
|
$monologConfigFile = $this->getMonologConfigFileName(); |
|
118
|
22 |
|
if (empty($monologConfigFile)) { |
|
119
|
|
|
return false; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
22 |
|
return $this->getTempDir().DIRECTORY_SEPARATOR.$monologConfigFile; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return mixed |
|
127
|
|
|
*/ |
|
128
|
22 |
|
private function getMonologConfigFileName() |
|
129
|
|
|
{ |
|
130
|
22 |
|
$monologConfig = $this->getMonologConfig(); |
|
131
|
|
|
|
|
132
|
22 |
|
if (isset($monologConfig['handlers']['file']['fileName'])) { |
|
133
|
22 |
|
return $monologConfig['handlers']['file']['fileName']; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return LoggerInterface |
|
139
|
|
|
*/ |
|
140
|
19 |
|
private function getLogger() |
|
141
|
|
|
{ |
|
142
|
19 |
|
return $this->logger; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param LoggerInterface $logger |
|
147
|
|
|
*/ |
|
148
|
22 |
|
private function setLogger(LoggerInterface $logger) |
|
149
|
|
|
{ |
|
150
|
22 |
|
$this->logger = $logger; |
|
151
|
22 |
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param string $function |
|
155
|
|
|
* @param string $message |
|
156
|
|
|
* @param string $channel |
|
157
|
|
|
* |
|
158
|
|
|
* @throws \Exception |
|
159
|
|
|
* |
|
160
|
|
|
* @return bool |
|
161
|
|
|
*/ |
|
162
|
11 |
|
public function logChat($function, $message = '', $channel = '') |
|
163
|
|
|
{ |
|
164
|
|
|
try { |
|
165
|
11 |
|
return $this->logInfo('Log Chat', [ |
|
166
|
11 |
|
'function' => $function, |
|
167
|
11 |
|
'message' => $message, |
|
168
|
11 |
|
'channel' => $channel, |
|
169
|
|
|
]); |
|
170
|
|
|
} catch (\Exception $e) { |
|
171
|
|
|
throw new \Exception($e->getMessage()); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @throws \Exception |
|
177
|
|
|
* |
|
178
|
|
|
* @return bool |
|
179
|
|
|
*/ |
|
180
|
21 |
|
private function canLog() |
|
181
|
|
|
{ |
|
182
|
21 |
|
$loggerConfig = $this->getConfig()->get('logger'); |
|
183
|
|
|
|
|
184
|
21 |
|
return empty($loggerConfig['enabled']) ? false : true; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @throws \Exception |
|
189
|
|
|
* |
|
190
|
|
|
* @return string |
|
191
|
|
|
*/ |
|
192
|
22 |
|
public function getTempDir() |
|
193
|
|
|
{ |
|
194
|
22 |
|
return dirname(__DIR__).DIRECTORY_SEPARATOR.self::TEMP_FOLDER; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @param $function |
|
199
|
|
|
* @param string $message |
|
200
|
|
|
* @param $channel |
|
201
|
|
|
* |
|
202
|
|
|
* @return string |
|
203
|
|
|
*/ |
|
204
|
1 |
|
public function getLogContent($function, $message, $channel) |
|
205
|
|
|
{ |
|
206
|
1 |
|
return "{$function}|{$message}|{$channel}"; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param $message |
|
211
|
|
|
* @param array $context |
|
212
|
|
|
* |
|
213
|
|
|
* @return bool |
|
214
|
|
|
*/ |
|
215
|
1 |
|
public function logDebug($message, array $context = []) |
|
216
|
|
|
{ |
|
217
|
1 |
|
return $this->log(self::DEBUG, $message, $context); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @param string $message |
|
222
|
|
|
* @param array $context |
|
223
|
|
|
* |
|
224
|
|
|
* @return bool |
|
225
|
|
|
*/ |
|
226
|
13 |
|
public function logInfo($message, array $context = []) |
|
227
|
|
|
{ |
|
228
|
13 |
|
return $this->log(self::INFO, $message, $context); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param $message |
|
233
|
|
|
* @param array $context |
|
234
|
|
|
* |
|
235
|
|
|
* @return bool |
|
236
|
|
|
*/ |
|
237
|
1 |
|
public function logNotice($message, array $context = []) |
|
238
|
|
|
{ |
|
239
|
1 |
|
return $this->log(self::NOTICE, $message, $context); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @param $message |
|
244
|
|
|
* @param array $context |
|
245
|
|
|
* |
|
246
|
|
|
* @return bool |
|
247
|
|
|
*/ |
|
248
|
1 |
|
public function logWarning($message, array $context = []) |
|
249
|
|
|
{ |
|
250
|
1 |
|
return $this->log(self::WARNING, $message, $context); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @param $message |
|
255
|
|
|
* @param array $context |
|
256
|
|
|
* |
|
257
|
|
|
* @return bool |
|
258
|
|
|
*/ |
|
259
|
1 |
|
public function logError($message, array $context = []) |
|
260
|
|
|
{ |
|
261
|
1 |
|
return $this->log(self::ERROR, $message, $context); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @param $message |
|
266
|
|
|
* @param array $context |
|
267
|
|
|
* |
|
268
|
|
|
* @return bool |
|
269
|
|
|
*/ |
|
270
|
1 |
|
public function logCritical($message, array $context = []) |
|
271
|
|
|
{ |
|
272
|
1 |
|
return $this->log(self::CRITICAL, $message, $context); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* @param $message |
|
277
|
|
|
* @param array $context |
|
278
|
|
|
* |
|
279
|
|
|
* @return bool |
|
280
|
|
|
*/ |
|
281
|
1 |
|
public function logAlert($message, array $context = []) |
|
282
|
|
|
{ |
|
283
|
1 |
|
return $this->log(self::ALERT, $message, $context); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @param $message |
|
288
|
|
|
* @param array $context |
|
289
|
|
|
* |
|
290
|
|
|
* @return bool |
|
291
|
|
|
*/ |
|
292
|
1 |
|
public function logEmergency($message, array $context = []) |
|
293
|
|
|
{ |
|
294
|
1 |
|
return $this->log(self::EMERGENCY, $message, $context); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* @param string $level |
|
299
|
|
|
* @param $message |
|
300
|
|
|
* @param array $context |
|
301
|
|
|
* |
|
302
|
|
|
* @throws \Exception |
|
303
|
|
|
* |
|
304
|
|
|
* @return bool |
|
305
|
|
|
*/ |
|
306
|
21 |
|
public function log($level, $message, $context = []) |
|
307
|
|
|
{ |
|
308
|
21 |
|
if ($this->canLog() !== true) { |
|
309
|
2 |
|
return false; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
19 |
|
$logger = $this->getLogger(); |
|
313
|
|
|
|
|
314
|
19 |
|
if (!in_array($level, self::$levels)) { |
|
315
|
1 |
|
throw new \Exception("'{$level}' is an invalid log level"); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
18 |
|
$logger->$level($message, $context); |
|
319
|
|
|
|
|
320
|
18 |
|
return true; |
|
321
|
|
|
} |
|
322
|
|
|
} |
|
323
|
|
|
|