1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\log; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\Component; |
12
|
|
|
use yii\base\InvalidConfigException; |
13
|
|
|
use yii\helpers\ArrayHelper; |
14
|
|
|
use yii\helpers\StringHelper; |
15
|
|
|
use yii\helpers\VarDumper; |
16
|
|
|
use yii\web\Request; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Target is the base class for all log target classes. |
20
|
|
|
* |
21
|
|
|
* A log target object will filter the messages logged by [[Logger]] according |
22
|
|
|
* to its [[levels]] and [[categories]] properties. It may also export the filtered |
23
|
|
|
* messages to specific destination defined by the target, such as emails, files. |
24
|
|
|
* |
25
|
|
|
* Level filter and category filter are combinatorial, i.e., only messages |
26
|
|
|
* satisfying both filter conditions will be handled. Additionally, you |
27
|
|
|
* may specify [[except]] to exclude messages of certain categories. |
28
|
|
|
* |
29
|
|
|
* @property bool $enabled Indicates whether this log target is enabled. Defaults to true. Note that the type |
30
|
|
|
* of this property differs in getter and setter. See [[getEnabled()]] and [[setEnabled()]] for details. |
31
|
|
|
* @property int $levels The message levels that this target is interested in. This is a bitmap of level |
32
|
|
|
* values. Defaults to 0, meaning all available levels. Note that the type of this property differs in getter and |
33
|
|
|
* setter. See [[getLevels()]] and [[setLevels()]] for details. |
34
|
|
|
* |
35
|
|
|
* For more details and usage information on Target, see the [guide article on logging & targets](guide:runtime-logging). |
36
|
|
|
* |
37
|
|
|
* @author Qiang Xue <[email protected]> |
38
|
|
|
* @since 2.0 |
39
|
|
|
*/ |
40
|
|
|
abstract class Target extends Component |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var array list of message categories that this target is interested in. Defaults to empty, meaning all categories. |
44
|
|
|
* You can use an asterisk at the end of a category so that the category may be used to |
45
|
|
|
* match those categories sharing the same common prefix. For example, 'yii\db\*' will match |
46
|
|
|
* categories starting with 'yii\db\', such as 'yii\db\Connection'. |
47
|
|
|
*/ |
48
|
|
|
public $categories = []; |
49
|
|
|
/** |
50
|
|
|
* @var array list of message categories that this target is NOT interested in. Defaults to empty, meaning no uninteresting messages. |
51
|
|
|
* If this property is not empty, then any category listed here will be excluded from [[categories]]. |
52
|
|
|
* You can use an asterisk at the end of a category so that the category can be used to |
53
|
|
|
* match those categories sharing the same common prefix. For example, 'yii\db\*' will match |
54
|
|
|
* categories starting with 'yii\db\', such as 'yii\db\Connection'. |
55
|
|
|
* @see categories |
56
|
|
|
*/ |
57
|
|
|
public $except = []; |
58
|
|
|
/** |
59
|
|
|
* @var array list of the PHP predefined variables that should be logged in a message. |
60
|
|
|
* Note that a variable must be accessible via `$GLOBALS`. Otherwise it won't be logged. |
61
|
|
|
* |
62
|
|
|
* Defaults to `['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER']`. |
63
|
|
|
* |
64
|
|
|
* Since version 2.0.9 additional syntax can be used: |
65
|
|
|
* Each element could be specified as one of the following: |
66
|
|
|
* |
67
|
|
|
* - `var` - `var` will be logged. |
68
|
|
|
* - `var.key` - only `var[key]` key will be logged. |
69
|
|
|
* - `!var.key` - `var[key]` key will be excluded. |
70
|
|
|
* |
71
|
|
|
* Note that if you need $_SESSION to logged regardless if session was used you have to open it right at |
72
|
|
|
* the start of your request. |
73
|
|
|
* |
74
|
|
|
* @see \yii\helpers\ArrayHelper::filter() |
75
|
|
|
*/ |
76
|
|
|
public $logVars = [ |
77
|
|
|
'_GET', |
78
|
|
|
'_POST', |
79
|
|
|
'_FILES', |
80
|
|
|
'_COOKIE', |
81
|
|
|
'_SESSION', |
82
|
|
|
'_SERVER', |
83
|
|
|
]; |
84
|
|
|
/** |
85
|
|
|
* @var array list of the PHP predefined variables that should NOT be logged "as is" and should always be replaced |
86
|
|
|
* with a mask `***` before logging, when exist. |
87
|
|
|
* |
88
|
|
|
* Defaults to `[ '_SERVER.HTTP_AUTHORIZATION', '_SERVER.PHP_AUTH_USER', '_SERVER.PHP_AUTH_PW']` |
89
|
|
|
* |
90
|
|
|
* Each element could be specified as one of the following: |
91
|
|
|
* |
92
|
|
|
* - `var` - `var` will be logged as `***` |
93
|
|
|
* - `var.key` - only `var[key]` will be logged as `***` |
94
|
|
|
* |
95
|
|
|
* @since 2.0.16 |
96
|
|
|
*/ |
97
|
|
|
public $maskVars = [ |
98
|
|
|
'_SERVER.HTTP_AUTHORIZATION', |
99
|
|
|
'_SERVER.PHP_AUTH_USER', |
100
|
|
|
'_SERVER.PHP_AUTH_PW', |
101
|
|
|
]; |
102
|
|
|
/** |
103
|
|
|
* @var callable a PHP callable that returns a string to be prefixed to every exported message. |
104
|
|
|
* |
105
|
|
|
* If not set, [[getMessagePrefix()]] will be used, which prefixes the message with context information |
106
|
|
|
* such as user IP, user ID and session ID. |
107
|
|
|
* |
108
|
|
|
* The signature of the callable should be `function ($message)`. |
109
|
|
|
*/ |
110
|
|
|
public $prefix; |
111
|
|
|
/** |
112
|
|
|
* @var int how many messages should be accumulated before they are exported. |
113
|
|
|
* Defaults to 1000. Note that messages will always be exported when the application terminates. |
114
|
|
|
* Set this property to be 0 if you don't want to export messages until the application terminates. |
115
|
|
|
*/ |
116
|
|
|
public $exportInterval = 1000; |
117
|
|
|
/** |
118
|
|
|
* @var array the messages that are retrieved from the logger so far by this log target. |
119
|
|
|
* Please refer to [[Logger::messages]] for the details about the message structure. |
120
|
|
|
*/ |
121
|
|
|
public $messages = []; |
122
|
|
|
/** |
123
|
|
|
* @var bool whether to log time with microseconds. |
124
|
|
|
* Defaults to false. |
125
|
|
|
* @since 2.0.13 |
126
|
|
|
*/ |
127
|
|
|
public $microtime = false; |
128
|
|
|
|
129
|
|
|
private $_levels = 0; |
130
|
|
|
private $_enabled = true; |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Exports log [[messages]] to a specific destination. |
135
|
|
|
* Child classes must implement this method. |
136
|
|
|
*/ |
137
|
|
|
abstract public function export(); |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Processes the given log messages. |
141
|
|
|
* This method will filter the given messages with [[levels]] and [[categories]]. |
142
|
|
|
* And if requested, it will also export the filtering result to specific medium (e.g. email). |
143
|
|
|
* @param array $messages log messages to be processed. See [[Logger::messages]] for the structure |
144
|
|
|
* of each message. |
145
|
|
|
* @param bool $final whether this method is called at the end of the current application |
146
|
|
|
*/ |
147
|
392 |
|
public function collect($messages, $final) |
148
|
|
|
{ |
149
|
392 |
|
$this->messages = array_merge($this->messages, static::filterMessages($messages, $this->getLevels(), $this->categories, $this->except)); |
150
|
392 |
|
$count = count($this->messages); |
151
|
392 |
|
if ($count > 0 && ($final || $this->exportInterval > 0 && $count >= $this->exportInterval)) { |
152
|
28 |
|
if (($context = $this->getContextMessage()) !== '') { |
153
|
7 |
|
$this->messages[] = [$context, Logger::LEVEL_INFO, 'application', YII_BEGIN_TIME, [], 0]; |
154
|
|
|
} |
155
|
|
|
// set exportInterval to 0 to avoid triggering export again while exporting |
156
|
28 |
|
$oldExportInterval = $this->exportInterval; |
157
|
28 |
|
$this->exportInterval = 0; |
158
|
28 |
|
$this->export(); |
159
|
28 |
|
$this->exportInterval = $oldExportInterval; |
160
|
|
|
|
161
|
28 |
|
$this->messages = []; |
162
|
|
|
} |
163
|
392 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Generates the context information to be logged. |
167
|
|
|
* The default implementation will dump user information, system variables, etc. |
168
|
|
|
* @return string the context information. If an empty string, it means no context information. |
169
|
|
|
*/ |
170
|
29 |
|
protected function getContextMessage() |
171
|
|
|
{ |
172
|
29 |
|
$context = ArrayHelper::filter($GLOBALS, $this->logVars); |
173
|
29 |
|
foreach ($this->maskVars as $var) { |
174
|
29 |
|
if (ArrayHelper::getValue($context, $var) !== null) { |
175
|
29 |
|
ArrayHelper::setValue($context, $var, '***'); |
176
|
|
|
} |
177
|
|
|
} |
178
|
29 |
|
$result = []; |
179
|
29 |
|
foreach ($context as $key => $value) { |
180
|
8 |
|
$result[] = "\${$key} = " . VarDumper::dumpAsString($value); |
181
|
|
|
} |
182
|
|
|
|
183
|
29 |
|
return implode("\n\n", $result); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return int the message levels that this target is interested in. This is a bitmap of |
188
|
|
|
* level values. Defaults to 0, meaning all available levels. |
189
|
|
|
*/ |
190
|
392 |
|
public function getLevels() |
191
|
|
|
{ |
192
|
392 |
|
return $this->_levels; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Sets the message levels that this target is interested in. |
197
|
|
|
* |
198
|
|
|
* The parameter can be either an array of interested level names or an integer representing |
199
|
|
|
* the bitmap of the interested level values. Valid level names include: 'error', |
200
|
|
|
* 'warning', 'info', 'trace' and 'profile'; valid level values include: |
201
|
|
|
* [[Logger::LEVEL_ERROR]], [[Logger::LEVEL_WARNING]], [[Logger::LEVEL_INFO]], |
202
|
|
|
* [[Logger::LEVEL_TRACE]] and [[Logger::LEVEL_PROFILE]]. |
203
|
|
|
* |
204
|
|
|
* For example, |
205
|
|
|
* |
206
|
|
|
* ```php |
207
|
|
|
* ['error', 'warning'] |
208
|
|
|
* // which is equivalent to: |
209
|
|
|
* Logger::LEVEL_ERROR | Logger::LEVEL_WARNING |
210
|
|
|
* ``` |
211
|
|
|
* |
212
|
|
|
* @param array|int $levels message levels that this target is interested in. |
213
|
|
|
* @throws InvalidConfigException if $levels value is not correct. |
214
|
|
|
*/ |
215
|
22 |
|
public function setLevels($levels) |
216
|
|
|
{ |
217
|
22 |
|
static $levelMap = [ |
218
|
|
|
'error' => Logger::LEVEL_ERROR, |
219
|
|
|
'warning' => Logger::LEVEL_WARNING, |
220
|
|
|
'info' => Logger::LEVEL_INFO, |
221
|
|
|
'trace' => Logger::LEVEL_TRACE, |
222
|
|
|
'profile' => Logger::LEVEL_PROFILE, |
223
|
|
|
]; |
224
|
22 |
|
if (is_array($levels)) { |
225
|
10 |
|
$this->_levels = 0; |
226
|
10 |
|
foreach ($levels as $level) { |
227
|
10 |
|
if (isset($levelMap[$level])) { |
228
|
10 |
|
$this->_levels |= $levelMap[$level]; |
229
|
|
|
} else { |
230
|
10 |
|
throw new InvalidConfigException("Unrecognized level: $level"); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
} else { |
234
|
12 |
|
$bitmapValues = array_reduce($levelMap, function ($carry, $item) { |
235
|
12 |
|
return $carry | $item; |
236
|
12 |
|
}); |
237
|
12 |
|
if (!($bitmapValues & $levels) && $levels !== 0) { |
238
|
|
|
throw new InvalidConfigException("Incorrect $levels value"); |
239
|
|
|
} |
240
|
12 |
|
$this->_levels = $levels; |
241
|
|
|
} |
242
|
22 |
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Filters the given messages according to their categories and levels. |
246
|
|
|
* @param array $messages messages to be filtered. |
247
|
|
|
* The message structure follows that in [[Logger::messages]]. |
248
|
|
|
* @param int $levels the message levels to filter by. This is a bitmap of |
249
|
|
|
* level values. Value 0 means allowing all levels. |
250
|
|
|
* @param array $categories the message categories to filter by. If empty, it means all categories are allowed. |
251
|
|
|
* @param array $except the message categories to exclude. If empty, it means all categories are allowed. |
252
|
|
|
* @return array the filtered messages. |
253
|
|
|
*/ |
254
|
392 |
|
public static function filterMessages($messages, $levels = 0, $categories = [], $except = []) |
255
|
|
|
{ |
256
|
392 |
|
foreach ($messages as $i => $message) { |
257
|
392 |
|
if ($levels && !($levels & $message[1])) { |
258
|
366 |
|
unset($messages[$i]); |
259
|
366 |
|
continue; |
260
|
|
|
} |
261
|
|
|
|
262
|
310 |
|
$matched = empty($categories); |
263
|
310 |
|
foreach ($categories as $category) { |
264
|
293 |
|
if ($message[2] === $category || !empty($category) && substr_compare($category, '*', -1, 1) === 0 && strpos($message[2], rtrim($category, '*')) === 0) { |
265
|
249 |
|
$matched = true; |
266
|
293 |
|
break; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
310 |
|
if ($matched) { |
271
|
266 |
|
foreach ($except as $category) { |
272
|
3 |
|
$prefix = rtrim($category, '*'); |
273
|
3 |
|
if (($message[2] === $category || $prefix !== $category) && strpos($message[2], $prefix) === 0) { |
274
|
3 |
|
$matched = false; |
275
|
3 |
|
break; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
310 |
|
if (!$matched) { |
281
|
310 |
|
unset($messages[$i]); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
392 |
|
return $messages; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Formats a log message for display as a string. |
290
|
|
|
* @param array $message the log message to be formatted. |
291
|
|
|
* The message structure follows that in [[Logger::messages]]. |
292
|
|
|
* @return string the formatted message |
293
|
|
|
*/ |
294
|
2 |
|
public function formatMessage($message) |
295
|
|
|
{ |
296
|
2 |
|
list($text, $level, $category, $timestamp) = $message; |
297
|
2 |
|
$level = Logger::getLevelName($level); |
298
|
2 |
|
if (!is_string($text)) { |
299
|
|
|
// exceptions may not be serializable if in the call stack somewhere is a Closure |
300
|
|
|
if ($text instanceof \Throwable || $text instanceof \Exception) { |
301
|
|
|
$text = (string) $text; |
302
|
|
|
} else { |
303
|
|
|
$text = VarDumper::export($text); |
304
|
|
|
} |
305
|
|
|
} |
306
|
2 |
|
$traces = []; |
307
|
2 |
|
if (isset($message[4])) { |
308
|
2 |
|
foreach ($message[4] as $trace) { |
309
|
|
|
$traces[] = "in {$trace['file']}:{$trace['line']}"; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
2 |
|
$prefix = $this->getMessagePrefix($message); |
314
|
2 |
|
return $this->getTime($timestamp) . " {$prefix}[$level][$category] $text" |
315
|
2 |
|
. (empty($traces) ? '' : "\n " . implode("\n ", $traces)); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Returns a string to be prefixed to the given message. |
320
|
|
|
* If [[prefix]] is configured it will return the result of the callback. |
321
|
|
|
* The default implementation will return user IP, user ID and session ID as a prefix. |
322
|
|
|
* @param array $message the message being exported. |
323
|
|
|
* The message structure follows that in [[Logger::messages]]. |
324
|
|
|
* @return string the prefix string |
325
|
|
|
*/ |
326
|
8 |
|
public function getMessagePrefix($message) |
327
|
|
|
{ |
328
|
8 |
|
if ($this->prefix !== null) { |
329
|
|
|
return call_user_func($this->prefix, $message); |
330
|
|
|
} |
331
|
|
|
|
332
|
8 |
|
if (Yii::$app === null) { |
333
|
|
|
return ''; |
334
|
|
|
} |
335
|
|
|
|
336
|
8 |
|
$request = Yii::$app->getRequest(); |
337
|
8 |
|
$ip = $request instanceof Request ? $request->getUserIP() : '-'; |
338
|
|
|
|
339
|
|
|
/* @var $user \yii\web\User */ |
340
|
8 |
|
$user = Yii::$app->has('user', true) ? Yii::$app->get('user') : null; |
341
|
8 |
|
if ($user && ($identity = $user->getIdentity(false))) { |
342
|
|
|
$userID = $identity->getId(); |
343
|
|
|
} else { |
344
|
8 |
|
$userID = '-'; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/* @var $session \yii\web\Session */ |
348
|
8 |
|
$session = Yii::$app->has('session', true) ? Yii::$app->get('session') : null; |
349
|
8 |
|
$sessionID = $session && $session->getIsActive() ? $session->getId() : '-'; |
350
|
|
|
|
351
|
8 |
|
return "[$ip][$userID][$sessionID]"; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Sets a value indicating whether this log target is enabled. |
356
|
|
|
* @param bool|callable $value a boolean value or a callable to obtain the value from. |
357
|
|
|
* The callable value is available since version 2.0.13. |
358
|
|
|
* |
359
|
|
|
* A callable may be used to determine whether the log target should be enabled in a dynamic way. |
360
|
|
|
* For example, to only enable a log if the current user is logged in you can configure the target |
361
|
|
|
* as follows: |
362
|
|
|
* |
363
|
|
|
* ```php |
364
|
|
|
* 'enabled' => function() { |
365
|
|
|
* return !Yii::$app->user->isGuest; |
366
|
|
|
* } |
367
|
|
|
* ``` |
368
|
|
|
*/ |
369
|
|
|
public function setEnabled($value) |
370
|
|
|
{ |
371
|
|
|
$this->_enabled = $value; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Check whether the log target is enabled. |
376
|
|
|
* @property bool Indicates whether this log target is enabled. Defaults to true. |
377
|
|
|
* @return bool A value indicating whether this log target is enabled. |
378
|
|
|
*/ |
379
|
391 |
|
public function getEnabled() |
380
|
|
|
{ |
381
|
391 |
|
if (is_callable($this->_enabled)) { |
382
|
|
|
return call_user_func($this->_enabled, $this); |
|
|
|
|
383
|
|
|
} |
384
|
|
|
|
385
|
391 |
|
return $this->_enabled; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Returns formatted ('Y-m-d H:i:s') timestamp for message. |
390
|
|
|
* If [[microtime]] is configured to true it will return format 'Y-m-d H:i:s.u'. |
391
|
|
|
* @param float $timestamp |
392
|
|
|
* @return string |
393
|
|
|
* @since 2.0.13 |
394
|
|
|
*/ |
395
|
2 |
|
protected function getTime($timestamp) |
396
|
|
|
{ |
397
|
2 |
|
$parts = explode('.', sprintf('%F', $timestamp)); |
398
|
|
|
|
399
|
2 |
|
return date('Y-m-d H:i:s', $parts[0]) . ($this->microtime ? ('.' . $parts[1]) : ''); |
|
|
|
|
400
|
|
|
} |
401
|
|
|
} |
402
|
|
|
|