|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Todd Burry <[email protected]> |
|
4
|
|
|
* @copyright 2009-2015 Vanilla Forums Inc. |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Garden\Cli; |
|
9
|
|
|
|
|
10
|
|
|
use Psr\Log\InvalidArgumentException; |
|
11
|
|
|
use Psr\Log\LoggerInterface; |
|
12
|
|
|
use Psr\Log\LoggerTrait; |
|
13
|
|
|
use Psr\Log\LogLevel; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* A helper class to format CLI output in a log-style format. |
|
17
|
|
|
*/ |
|
18
|
|
|
class TaskLogger implements LoggerInterface { |
|
19
|
|
|
use LoggerTrait; |
|
20
|
|
|
|
|
21
|
|
|
const FIELD_INDENT = '_indent'; |
|
22
|
|
|
const FIELD_TIME = '_time'; |
|
23
|
|
|
const FIELD_BEGIN = '_begin'; |
|
24
|
|
|
const FIELD_END = '_end'; |
|
25
|
|
|
const FIELD_DURATION = '_duration'; |
|
26
|
|
|
const FIELD_LEVEL = '_level'; |
|
27
|
|
|
|
|
28
|
|
|
private static $levels = [ |
|
29
|
|
|
LogLevel::DEBUG, |
|
30
|
|
|
LogLevel::INFO, |
|
31
|
|
|
LogLevel::NOTICE, |
|
32
|
|
|
LogLevel::WARNING, |
|
33
|
|
|
LogLevel::ERROR, |
|
34
|
|
|
LogLevel::CRITICAL, |
|
35
|
|
|
LogLevel::ALERT, |
|
36
|
|
|
LogLevel::EMERGENCY, |
|
37
|
|
|
]; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var int The minimum level deep to output. |
|
40
|
|
|
*/ |
|
41
|
|
|
private $minLevel = LogLevel::INFO; |
|
42
|
|
|
/** |
|
43
|
|
|
* @var array An array of currently running tasks. |
|
44
|
|
|
*/ |
|
45
|
|
|
private $taskStack = []; |
|
46
|
|
|
/** |
|
47
|
|
|
* @var LoggerInterface The logger to ultimately log the information to. |
|
48
|
|
|
*/ |
|
49
|
|
|
private $logger; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* TaskLogger constructor. |
|
53
|
|
|
* |
|
54
|
|
|
* @param LoggerInterface $logger The logger to ultimately log the information to. |
|
55
|
|
|
* @param string $minLevel The minimum error level that will be logged. One of the **LogLevel** constants. |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct(LoggerInterface $logger = null, $minLevel = LogLevel::INFO) { |
|
58
|
|
|
if ($logger === null) { |
|
59
|
|
|
$logger = new StreamLogger(); |
|
60
|
|
|
} |
|
61
|
|
|
$this->logger = $logger; |
|
62
|
|
|
$this->setMinLevel($minLevel); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Output a debug message that designates the beginning of a task. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $message The message to output. |
|
69
|
|
|
* @param array $context Context variables to pass to the message. |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
|
|
public function beginDebug(string $message, array $context = []) { |
|
73
|
|
|
return $this->begin(LogLevel::DEBUG, $message, $context); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Output a message that designates the beginning of a task. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $level |
|
80
|
|
|
* @param string $message The message to output. |
|
81
|
|
|
* @param array $context The log context. |
|
82
|
|
|
* @return $this |
|
83
|
|
|
*/ |
|
84
|
|
|
public function begin(string $level, string $message, array $context = []) { |
|
85
|
|
|
$output = $this->compareLevel($level, $this->getMinLevel()) >= 0; |
|
86
|
|
|
$context = [self::FIELD_BEGIN => true] + $context; |
|
87
|
|
|
$task = [$level, $message, $context, $output]; |
|
88
|
|
|
|
|
89
|
|
|
if ($output) { |
|
90
|
|
|
$this->log($level, $message, $context); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
array_push($this->taskStack, $task); |
|
94
|
|
|
|
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Compare two log levels. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $a The first log level to compare. |
|
102
|
|
|
* @param string $b The second log level to compare. |
|
103
|
|
|
* @return int Returns -1, 0, or 1. |
|
104
|
|
|
*/ |
|
105
|
|
|
private function compareLevel(string $a, string $b): int { |
|
106
|
|
|
$i = array_search($a, static::$levels); |
|
|
|
|
|
|
107
|
|
|
if ($i === false) { |
|
108
|
|
|
throw new InvalidArgumentException("Log level is invalid: $a", 500); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $i <=> array_search($b, static::$levels); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get the maxLevel. |
|
116
|
|
|
* |
|
117
|
|
|
* @return string Returns the maxLevel. |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getMinLevel(): string { |
|
120
|
|
|
return $this->minLevel; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param string $minLevel |
|
125
|
|
|
* @return $this |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setMinLevel(string $minLevel) { |
|
128
|
|
|
$this->minLevel = $minLevel; |
|
|
|
|
|
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Logs with an arbitrary level. |
|
134
|
|
|
* |
|
135
|
|
|
* @param mixed $level |
|
136
|
|
|
* @param string $message |
|
137
|
|
|
* @param array $context |
|
138
|
|
|
* |
|
139
|
|
|
* @return void |
|
140
|
|
|
*/ |
|
141
|
|
|
public function log($level, $message, array $context = []) { |
|
142
|
|
|
if ($this->compareLevel($level, $this->getMinLevel()) >= 0) { |
|
143
|
|
|
$this->outputTaskStack(); |
|
144
|
|
|
|
|
145
|
|
|
// Increase the level begin tasks if this level is higher. |
|
146
|
|
|
foreach ($this->taskStack as &$task) { |
|
147
|
|
|
if ($this->compareLevel($level, $task[0]) > 0) { |
|
148
|
|
|
$task[0] = $level; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$this->logInternal($level, $message, [self::FIELD_INDENT => $this->currentIndent()] + $context); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Output the task stack. |
|
158
|
|
|
*/ |
|
159
|
|
|
private function outputTaskStack() { |
|
160
|
|
|
foreach ($this->taskStack as $indent => &$task) { |
|
161
|
|
|
list($taskLevel, $taskMessage, $taskContext, $taskOutput) = $task; |
|
162
|
|
|
if (!$taskOutput) { |
|
163
|
|
|
$this->logInternal($taskLevel, $taskMessage, [self::FIELD_INDENT => $indent] + $taskContext); |
|
164
|
|
|
|
|
165
|
|
|
$task[3] = true; // mark task as outputted |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Internal log implementation with less error checking. |
|
172
|
|
|
* |
|
173
|
|
|
* @param string $level The log level. |
|
174
|
|
|
* @param string $message The log message. |
|
175
|
|
|
* @param array $context The log context. |
|
176
|
|
|
*/ |
|
177
|
|
|
private function logInternal(string $level, string $message, array $context = []) { |
|
178
|
|
|
$context = $context + [self::FIELD_INDENT => $this->currentIndent(), self::FIELD_TIME => microtime(1)]; |
|
179
|
|
|
$this->logger->log($level, $message, $context); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Get the current depth of tasks. |
|
184
|
|
|
* |
|
185
|
|
|
* @return int Returns the current level. |
|
186
|
|
|
*/ |
|
187
|
|
|
private function currentIndent() { |
|
188
|
|
|
return count($this->taskStack); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Output an info message that designates the beginning of a task. |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $message The message to output. |
|
195
|
|
|
* @param array $context Context variables to pass to the message. |
|
196
|
|
|
* @return $this |
|
197
|
|
|
*/ |
|
198
|
|
|
public function beginInfo(string $message, array $context = []) { |
|
199
|
|
|
return $this->begin(LogLevel::INFO, $message, $context); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Output a notice message that designates the beginning of a task. |
|
204
|
|
|
* |
|
205
|
|
|
* @param string $message The message to output. |
|
206
|
|
|
* @param array $context Context variables to pass to the message. |
|
207
|
|
|
* @return $this |
|
208
|
|
|
*/ |
|
209
|
|
|
public function beginNotice(string $message, array $context = []) { |
|
210
|
|
|
return $this->begin(LogLevel::NOTICE, $message, $context); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Output a warning message that designates the beginning of a task. |
|
215
|
|
|
* |
|
216
|
|
|
* @param string $message The message to output. |
|
217
|
|
|
* @param array $context Context variables to pass to the message. |
|
218
|
|
|
* @return $this |
|
219
|
|
|
*/ |
|
220
|
|
|
public function beginWarning(string $message, array $context = []) { |
|
221
|
|
|
return $this->begin(LogLevel::WARNING, $message, $context); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Output an error message that designates the beginning of a task. |
|
226
|
|
|
* |
|
227
|
|
|
* @param string $message The message to output. |
|
228
|
|
|
* @param array $context Context variables to pass to the message. |
|
229
|
|
|
* @return $this |
|
230
|
|
|
*/ |
|
231
|
|
|
public function beginError(string $message, array $context = []) { |
|
232
|
|
|
return $this->begin(LogLevel::ERROR, $message, $context); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Output a critical message that designates the beginning of a task. |
|
237
|
|
|
* |
|
238
|
|
|
* @param string $message The message to output. |
|
239
|
|
|
* @param array $context Context variables to pass to the message. |
|
240
|
|
|
* @return $this |
|
241
|
|
|
*/ |
|
242
|
|
|
public function beginCritical(string $message, array $context = []) { |
|
243
|
|
|
return $this->begin(LogLevel::CRITICAL, $message, $context); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Output an alert message that designates the beginning of a task. |
|
248
|
|
|
* |
|
249
|
|
|
* @param string $message The message to output. |
|
250
|
|
|
* @param array $context Context variables to pass to the message. |
|
251
|
|
|
* @return $this |
|
252
|
|
|
*/ |
|
253
|
|
|
public function beginAlert(string $message, array $context = []) { |
|
254
|
|
|
return $this->begin(LogLevel::ALERT, $message, $context); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Output an emergency message that designates the beginning of a task. |
|
259
|
|
|
* |
|
260
|
|
|
* @param string $message The message to output. |
|
261
|
|
|
* @param array $context Context variables to pass to the message. |
|
262
|
|
|
* @return $this |
|
263
|
|
|
*/ |
|
264
|
|
|
public function beginEmergency(string $message, array $context = []) { |
|
265
|
|
|
return $this->begin(LogLevel::EMERGENCY, $message, $context); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Output a message that ends a task with an HTTP status code. |
|
270
|
|
|
* |
|
271
|
|
|
* This method is useful if you are making a call to an external API as a task. You can end the task by passing the |
|
272
|
|
|
* response code to this message. |
|
273
|
|
|
* |
|
274
|
|
|
* @param int $httpStatus The HTTP status code that represents the completion of a task. |
|
275
|
|
|
* @return $this |
|
276
|
|
|
*/ |
|
277
|
|
|
public function endHttpStatus(int $httpStatus) { |
|
278
|
|
|
$statusStr = sprintf('%03d', $httpStatus); |
|
279
|
|
|
|
|
280
|
|
|
if ($httpStatus == 0 || $httpStatus >= 500) { |
|
281
|
|
|
$this->end($statusStr, [self::FIELD_LEVEL => LogLevel::CRITICAL]); |
|
282
|
|
|
} elseif ($httpStatus >= 400) { |
|
283
|
|
|
$this->endError($statusStr); |
|
284
|
|
|
} else { |
|
285
|
|
|
$this->end($statusStr); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
return $this; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* Output a message that designates a task being completed. |
|
293
|
|
|
* |
|
294
|
|
|
* @param string $message The message to output. |
|
295
|
|
|
* @param array $context Context for the log. There are a few special fields that can be given with the context. |
|
296
|
|
|
* |
|
297
|
|
|
* - **TaskLogger::FIELD_TIME**: Specify a specific timestamp for the log. |
|
298
|
|
|
* - **TaskLogger::FIELD_LEVEL**: Override the level of the end log. Otherwise the level of the beg log is used. |
|
299
|
|
|
* |
|
300
|
|
|
* @return $this Returns `$this` for fluent calls. |
|
301
|
|
|
*/ |
|
302
|
|
|
public function end(string $message = '', array $context = []) { |
|
303
|
|
|
$context = [self::FIELD_INDENT => $this->currentIndent() - 1, self::FIELD_END => true] + $context + [self::FIELD_TIME => microtime(true)]; |
|
304
|
|
|
$level = $context[self::FIELD_LEVEL] ?? null; |
|
305
|
|
|
|
|
306
|
|
|
// Find the task we are finishing. |
|
307
|
|
|
$task = end($this->taskStack); |
|
308
|
|
|
if ($task !== false) { |
|
309
|
|
|
list($taskLevel, $taskMessage, $taskContext, $taskOutput) = $task; |
|
310
|
|
|
$context[self::FIELD_DURATION] = $context[self::FIELD_TIME] - $taskContext[self::FIELD_TIME]; |
|
311
|
|
|
$level = $level ?: $taskLevel; |
|
312
|
|
|
} else { |
|
313
|
|
|
trigger_error('Called TaskLogger::end() without calling TaskFormatter::begin()', E_USER_NOTICE); |
|
314
|
|
|
} |
|
315
|
|
|
$level = $level ?: LogLevel::INFO; |
|
316
|
|
|
|
|
317
|
|
|
$output = $this->compareLevel($level, $this->getMinLevel()) >= 0; |
|
318
|
|
|
if ($output) { |
|
319
|
|
|
$this->logInternal($level, $message, $context); |
|
320
|
|
|
} |
|
321
|
|
|
array_pop($this->taskStack); |
|
322
|
|
|
|
|
323
|
|
|
return $this; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* Output a message that represents a task being completed in an error. |
|
328
|
|
|
* |
|
329
|
|
|
* When formatting is turned on, error messages are output in red. |
|
330
|
|
|
* |
|
331
|
|
|
* @param string $message The message to output. |
|
332
|
|
|
* @param array $context The log context. |
|
333
|
|
|
* @return $this |
|
334
|
|
|
*/ |
|
335
|
|
|
public function endError(string $message, array $context = []) { |
|
336
|
|
|
return $this->end($message, [self::FIELD_LEVEL => LogLevel::ERROR] + $context); |
|
337
|
|
|
} |
|
338
|
|
|
} |
|
339
|
|
|
|