1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Botonomous\utility; |
4
|
|
|
|
5
|
|
|
use Botonomous\BotonomousException; |
6
|
|
|
use Monolog\Handler\StreamHandler; |
7
|
|
|
use Monolog\Logger; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class LoggerUtility. |
12
|
|
|
*/ |
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 |
143
|
|
|
{ |
144
|
19 |
|
return $this->logger; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param LoggerInterface $logger |
149
|
|
|
*/ |
150
|
22 |
|
private function setLogger(LoggerInterface $logger) |
151
|
|
|
{ |
152
|
22 |
|
$this->logger = $logger; |
153
|
22 |
|
} |
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 |
165
|
|
|
{ |
166
|
|
|
try { |
167
|
11 |
|
return $this->logInfo('Log Chat', [ |
168
|
11 |
|
'function' => $function, |
169
|
11 |
|
'message' => $message, |
170
|
11 |
|
'channel' => $channel, |
171
|
|
|
]); |
172
|
|
|
} catch (\Exception $e) { |
173
|
|
|
throw new BotonomousException($e->getMessage()); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @throws \Exception |
179
|
|
|
* |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
21 |
|
private function canLog(): bool |
183
|
|
|
{ |
184
|
21 |
|
$loggerConfig = $this->getConfig()->get('logger'); |
185
|
|
|
|
186
|
21 |
|
return empty($loggerConfig['enabled']) ? false : true; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @throws \Exception |
191
|
|
|
* |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
22 |
|
public function getTempDir(): string |
195
|
|
|
{ |
196
|
22 |
|
return dirname(__DIR__).DIRECTORY_SEPARATOR.self::TEMP_FOLDER; |
197
|
|
|
} |
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 |
207
|
|
|
{ |
208
|
1 |
|
return "{$function}|{$message}|{$channel}"; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param $message |
213
|
|
|
* @param array $context |
214
|
|
|
* |
215
|
|
|
* @return bool |
216
|
|
|
*/ |
217
|
1 |
|
public function logDebug(string $message, array $context = []): bool |
218
|
|
|
{ |
219
|
1 |
|
return $this->log(self::DEBUG, $message, $context); |
220
|
|
|
} |
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 |
229
|
|
|
{ |
230
|
13 |
|
return $this->log(self::INFO, $message, $context); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param $message |
235
|
|
|
* @param array $context |
236
|
|
|
* |
237
|
|
|
* @return bool |
238
|
|
|
*/ |
239
|
1 |
|
public function logNotice(string $message, array $context = []): bool |
240
|
|
|
{ |
241
|
1 |
|
return $this->log(self::NOTICE, $message, $context); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param $message |
246
|
|
|
* @param array $context |
247
|
|
|
* |
248
|
|
|
* @return bool |
249
|
|
|
*/ |
250
|
1 |
|
public function logWarning(string $message, array $context = []): bool |
251
|
|
|
{ |
252
|
1 |
|
return $this->log(self::WARNING, $message, $context); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param $message |
257
|
|
|
* @param array $context |
258
|
|
|
* |
259
|
|
|
* @return bool |
260
|
|
|
*/ |
261
|
1 |
|
public function logError(string $message, array $context = []): bool |
262
|
|
|
{ |
263
|
1 |
|
return $this->log(self::ERROR, $message, $context); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param $message |
268
|
|
|
* @param array $context |
269
|
|
|
* |
270
|
|
|
* @return bool |
271
|
|
|
*/ |
272
|
1 |
|
public function logCritical(string $message, array $context = []): bool |
273
|
|
|
{ |
274
|
1 |
|
return $this->log(self::CRITICAL, $message, $context); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param $message |
279
|
|
|
* @param array $context |
280
|
|
|
* |
281
|
|
|
* @return bool |
282
|
|
|
*/ |
283
|
1 |
|
public function logAlert(string $message, array $context = []): bool |
284
|
|
|
{ |
285
|
1 |
|
return $this->log(self::ALERT, $message, $context); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @param $message |
290
|
|
|
* @param array $context |
291
|
|
|
* |
292
|
|
|
* @return bool |
293
|
|
|
*/ |
294
|
1 |
|
public function logEmergency(string $message, array $context = []): bool |
295
|
|
|
{ |
296
|
1 |
|
return $this->log(self::EMERGENCY, $message, $context); |
297
|
|
|
} |
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 |
309
|
|
|
{ |
310
|
21 |
|
if ($this->canLog() !== true) { |
311
|
2 |
|
return false; |
312
|
|
|
} |
313
|
|
|
|
314
|
19 |
|
$logger = $this->getLogger(); |
315
|
|
|
|
316
|
19 |
|
if (!in_array($level, self::$levels)) { |
317
|
1 |
|
throw new BotonomousException("'{$level}' is an invalid log level"); |
318
|
|
|
} |
319
|
|
|
|
320
|
18 |
|
$logger->$level($message, $context); |
321
|
|
|
|
322
|
18 |
|
return true; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|