|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the TelegramBot package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Longman\TelegramBot; |
|
12
|
|
|
|
|
13
|
|
|
use Longman\TelegramBot\Exception\TelegramLogException; |
|
14
|
|
|
use Monolog\Formatter\LineFormatter; |
|
15
|
|
|
use Monolog\Handler\StreamHandler; |
|
16
|
|
|
use Monolog\Logger; |
|
17
|
|
|
|
|
18
|
|
|
class TelegramLog |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Monolog instance |
|
22
|
|
|
* |
|
23
|
|
|
* @var Logger |
|
24
|
|
|
*/ |
|
25
|
|
|
protected static $monolog; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Monolog instance for update |
|
29
|
|
|
* |
|
30
|
|
|
* @var Logger |
|
31
|
|
|
*/ |
|
32
|
|
|
protected static $monolog_update; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Path for error log |
|
36
|
|
|
* |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected static $error_log_path; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Path for debug log |
|
43
|
|
|
* |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected static $debug_log_path; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Path for update log |
|
50
|
|
|
* |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected static $update_log_path; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Temporary stream handle for debug log |
|
57
|
|
|
* |
|
58
|
|
|
* @var resource|null |
|
59
|
|
|
*/ |
|
60
|
|
|
protected static $debug_log_temp_stream_handle; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Initialize Monolog Logger instance, optionally passing an existing one |
|
64
|
|
|
* |
|
65
|
|
|
* @param Logger |
|
66
|
|
|
* |
|
67
|
|
|
* @return Logger |
|
68
|
|
|
*/ |
|
69
|
3 |
|
public static function initialize(Logger $external_monolog = null) |
|
70
|
|
|
{ |
|
71
|
3 |
|
if (self::$monolog === null) { |
|
72
|
3 |
|
if ($external_monolog !== null) { |
|
73
|
1 |
|
self::$monolog = $external_monolog; |
|
74
|
|
|
|
|
75
|
1 |
|
foreach (self::$monolog->getHandlers() as $handler) { |
|
76
|
1 |
|
if (method_exists($handler, 'getLevel') && $handler->getLevel() === 400) { |
|
77
|
1 |
|
self::$error_log_path = 'true'; |
|
78
|
|
|
} |
|
79
|
1 |
|
if (method_exists($handler, 'getLevel') && $handler->getLevel() === 100) { |
|
80
|
1 |
|
self::$debug_log_path = 'true'; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} else { |
|
84
|
2 |
|
self::$monolog = new Logger('bot_log'); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
3 |
|
return self::$monolog; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Initialize error log |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $path |
|
95
|
|
|
* |
|
96
|
|
|
* @return Logger |
|
97
|
|
|
* @throws TelegramLogException |
|
98
|
|
|
* @throws \InvalidArgumentException |
|
99
|
|
|
* @throws \Exception |
|
100
|
|
|
*/ |
|
101
|
2 |
|
public static function initErrorLog($path) |
|
102
|
|
|
{ |
|
103
|
2 |
|
if ($path === null || $path === '') { |
|
104
|
1 |
|
throw new TelegramLogException('Empty path for error log'); |
|
105
|
|
|
} |
|
106
|
1 |
|
self::initialize(); |
|
107
|
1 |
|
self::$error_log_path = $path; |
|
108
|
|
|
|
|
109
|
1 |
|
return self::$monolog->pushHandler( |
|
110
|
1 |
|
(new StreamHandler(self::$error_log_path, Logger::ERROR)) |
|
111
|
1 |
|
->setFormatter(new LineFormatter(null, null, true)) |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Initialize debug log |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $path |
|
119
|
|
|
* |
|
120
|
|
|
* @return Logger |
|
121
|
|
|
* @throws TelegramLogException |
|
122
|
|
|
* @throws \InvalidArgumentException |
|
123
|
|
|
* @throws \Exception |
|
124
|
|
|
*/ |
|
125
|
2 |
|
public static function initDebugLog($path) |
|
126
|
|
|
{ |
|
127
|
2 |
|
if ($path === null || $path === '') { |
|
128
|
1 |
|
throw new TelegramLogException('Empty path for debug log'); |
|
129
|
|
|
} |
|
130
|
1 |
|
self::initialize(); |
|
131
|
1 |
|
self::$debug_log_path = $path; |
|
132
|
|
|
|
|
133
|
1 |
|
return self::$monolog->pushHandler( |
|
134
|
1 |
|
(new StreamHandler(self::$debug_log_path, Logger::DEBUG)) |
|
135
|
1 |
|
->setFormatter(new LineFormatter(null, null, true)) |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get the stream handle of the temporary debug output |
|
141
|
|
|
* |
|
142
|
|
|
* @return mixed The stream if debug is active, else false |
|
143
|
|
|
*/ |
|
144
|
|
|
public static function getDebugLogTempStream() |
|
145
|
|
|
{ |
|
146
|
|
|
if (self::$debug_log_temp_stream_handle === null) { |
|
147
|
|
|
if (!self::isDebugLogActive()) { |
|
148
|
|
|
return false; |
|
149
|
|
|
} |
|
150
|
|
|
if ($temp_stream_handle = fopen('php://temp', 'wb+')) { |
|
151
|
|
|
self::$debug_log_temp_stream_handle = $temp_stream_handle; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return self::$debug_log_temp_stream_handle; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Write the temporary debug stream to log and close the stream handle |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $message Message (with placeholder) to write to the debug log |
|
162
|
|
|
*/ |
|
163
|
|
|
public static function endDebugLogTempStream($message = '%s') |
|
164
|
|
|
{ |
|
165
|
|
|
if (is_resource(self::$debug_log_temp_stream_handle)) { |
|
166
|
|
|
rewind(self::$debug_log_temp_stream_handle); |
|
167
|
|
|
self::debug($message, stream_get_contents(self::$debug_log_temp_stream_handle)); |
|
168
|
|
|
fclose(self::$debug_log_temp_stream_handle); |
|
169
|
|
|
self::$debug_log_temp_stream_handle = null; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Initialize update log |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $path |
|
177
|
|
|
* |
|
178
|
|
|
* @return Logger |
|
179
|
|
|
* @throws TelegramLogException |
|
180
|
|
|
* @throws \InvalidArgumentException |
|
181
|
|
|
* @throws \Exception |
|
182
|
|
|
*/ |
|
183
|
2 |
|
public static function initUpdateLog($path) |
|
184
|
|
|
{ |
|
185
|
2 |
|
if ($path === null || $path === '') { |
|
186
|
1 |
|
throw new TelegramLogException('Empty path for update log'); |
|
187
|
|
|
} |
|
188
|
1 |
|
self::$update_log_path = $path; |
|
189
|
|
|
|
|
190
|
1 |
|
if (self::$monolog_update === null) { |
|
191
|
1 |
|
self::$monolog_update = new Logger('bot_update_log'); |
|
192
|
|
|
|
|
193
|
1 |
|
self::$monolog_update->pushHandler( |
|
194
|
1 |
|
(new StreamHandler(self::$update_log_path, Logger::INFO)) |
|
195
|
1 |
|
->setFormatter(new LineFormatter('%message%' . PHP_EOL)) |
|
196
|
|
|
); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
1 |
|
return self::$monolog_update; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Is error log active |
|
204
|
|
|
* |
|
205
|
|
|
* @return bool |
|
206
|
|
|
*/ |
|
207
|
4 |
|
public static function isErrorLogActive() |
|
208
|
|
|
{ |
|
209
|
4 |
|
return self::$error_log_path !== null; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Is debug log active |
|
214
|
|
|
* |
|
215
|
|
|
* @return bool |
|
216
|
|
|
*/ |
|
217
|
2 |
|
public static function isDebugLogActive() |
|
218
|
|
|
{ |
|
219
|
2 |
|
return self::$debug_log_path !== null; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Is update log active |
|
224
|
|
|
* |
|
225
|
|
|
* @return bool |
|
226
|
|
|
*/ |
|
227
|
1 |
|
public static function isUpdateLogActive() |
|
228
|
|
|
{ |
|
229
|
1 |
|
return self::$update_log_path !== null; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Report error log |
|
234
|
|
|
* |
|
235
|
|
|
* @param string $text |
|
236
|
|
|
*/ |
|
237
|
4 |
|
public static function error($text) |
|
238
|
|
|
{ |
|
239
|
4 |
|
if (self::isErrorLogActive()) { |
|
240
|
4 |
|
$text = self::getLogText($text, func_get_args()); |
|
241
|
4 |
|
self::$monolog->error($text); |
|
242
|
|
|
} |
|
243
|
4 |
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Report debug log |
|
247
|
|
|
* |
|
248
|
|
|
* @param string $text |
|
249
|
|
|
*/ |
|
250
|
2 |
|
public static function debug($text) |
|
251
|
|
|
{ |
|
252
|
2 |
|
if (self::isDebugLogActive()) { |
|
253
|
2 |
|
$text = self::getLogText($text, func_get_args()); |
|
254
|
2 |
|
self::$monolog->debug($text); |
|
255
|
|
|
} |
|
256
|
2 |
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Report update log |
|
260
|
|
|
* |
|
261
|
|
|
* @param string $text |
|
262
|
|
|
*/ |
|
263
|
1 |
|
public static function update($text) |
|
264
|
|
|
{ |
|
265
|
1 |
|
if (self::isUpdateLogActive()) { |
|
266
|
1 |
|
$text = self::getLogText($text, func_get_args()); |
|
267
|
1 |
|
self::$monolog_update->info($text); |
|
268
|
|
|
} |
|
269
|
1 |
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Applies vsprintf to the text if placeholder replacements are passed along. |
|
273
|
|
|
* |
|
274
|
|
|
* @param string $text |
|
275
|
|
|
* @param array $args |
|
276
|
|
|
* |
|
277
|
|
|
* @return string |
|
278
|
|
|
*/ |
|
279
|
6 |
|
protected static function getLogText($text, array $args = []) |
|
280
|
|
|
{ |
|
281
|
|
|
// Pop the $text off the array, as it gets passed via func_get_args(). |
|
282
|
6 |
|
array_shift($args); |
|
283
|
|
|
|
|
284
|
|
|
// If no placeholders have been passed, don't parse the text. |
|
285
|
6 |
|
if (empty($args)) { |
|
286
|
4 |
|
return $text; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
6 |
|
return vsprintf($text, $args); |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
|