描述
在使用 Yii::info('xx', 'xx');的时候,你会发现处理category xxx之外还有一条 application的日志。
这条日志是如何产生的?
vendor/yiisoft/yii2/log/Target.php 的 $context = ArrayHelper::filter($GLOBALS, $this->logVars); 产生了数据,使得 [$context, Logger::LEVEL_INFO, ‘application’, YII_BEGIN_TIME, [], 0];
public function collect($messages, $final){$this->messages = array_merge($this->messages, static::filterMessages($messages, $this->getLevels(), $this->categories, $this->except));$count = count($this->messages);if ($count > 0 && ($final || $this->exportInterval > 0 && $count >= $this->exportInterval)) {if (($context = $this->getContextMessage()) !== '') {$this->messages[] = [$context, Logger::LEVEL_INFO, 'application', YII_BEGIN_TIME, [], 0];}// set exportInterval to 0 to avoid triggering export again while exporting$oldExportInterval = $this->exportInterval;$this->exportInterval = 0;$this->export();$this->exportInterval = $oldExportInterval;$this->messages = [];}}
protected function getContextMessage(){$context = ArrayHelper::filter($GLOBALS, $this->logVars);foreach ($this->maskVars as $var) {if (ArrayHelper::getValue($context, $var) !== null) {ArrayHelper::setValue($context, $var, '***');}}$result = [];foreach ($context as $key => $value) {$result[] = "\${$key} = " . VarDumper::dumpAsString($value);}return implode("\n\n", $result);}
方案:
所以解决方案是:由 'logVars'=>['_GET', '_POST'] 改为 'logVars => []',
但是,如果还要记录get,post请求,只能主动将信息也主动写入,如:
Yii::info(['message' => '某日志', 'get' => Yii::$app->request->get(),], '某target');
