与此要求一致 https://www.yiichina.com/question/3084

描述

在使用 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];

  1. public function collect($messages, $final)
  2. {
  3. $this->messages = array_merge($this->messages, static::filterMessages($messages, $this->getLevels(), $this->categories, $this->except));
  4. $count = count($this->messages);
  5. if ($count > 0 && ($final || $this->exportInterval > 0 && $count >= $this->exportInterval)) {
  6. if (($context = $this->getContextMessage()) !== '') {
  7. $this->messages[] = [$context, Logger::LEVEL_INFO, 'application', YII_BEGIN_TIME, [], 0];
  8. }
  9. // set exportInterval to 0 to avoid triggering export again while exporting
  10. $oldExportInterval = $this->exportInterval;
  11. $this->exportInterval = 0;
  12. $this->export();
  13. $this->exportInterval = $oldExportInterval;
  14. $this->messages = [];
  15. }
  16. }
  1. protected function getContextMessage()
  2. {
  3. $context = ArrayHelper::filter($GLOBALS, $this->logVars);
  4. foreach ($this->maskVars as $var) {
  5. if (ArrayHelper::getValue($context, $var) !== null) {
  6. ArrayHelper::setValue($context, $var, '***');
  7. }
  8. }
  9. $result = [];
  10. foreach ($context as $key => $value) {
  11. $result[] = "\${$key} = " . VarDumper::dumpAsString($value);
  12. }
  13. return implode("\n\n", $result);
  14. }

方案:

所以解决方案是:由 'logVars'=>['_GET', '_POST'] 改为 'logVars => []',
但是,如果还要记录get,post请求,只能主动将信息也主动写入,如:

  1. Yii::info(['message' => '某日志', 'get' => Yii::$app->request->get(),], '某target');