【问题标题】:Log not working in yii2日志在yii2中不起作用
【发布时间】:2016-10-05 10:40:34
【问题描述】:

我想在 app.log 中添加一个日志,我的配置文件

 'log' => [
        'traceLevel' => YII_DEBUG ? 3 : 0,
        'targets' => [
            'file' => [
                'class' => 'yii\log\FileTarget',
                'levels' => ['error', 'warning'],
                'logFile' => '@root/console/runtime/logs/app.log',
            ],
        ]
    ]

在控制器动作中

 public function actionRankCalculation()
{
    $allConest = Contest::find()->where('isActive = 1')->all();
    Yii::trace('start calculating average revenue');
    $response = [];
    /** @var Contest $contest */
    foreach ($allConest as $contest) {
        $videoQuery = Video::find()->where('contest_id = ' . $contest->id);
        $videoQuery->andWhere('isActive = 1');
        $videoQuery->orderBy([
            'global_likes' => SORT_DESC,
            'id' => SORT_ASC,
        ]);

} 但是 Yii::trace('开始计算平均收益');不工作

【问题讨论】:

  • 如果您的path alias 正确,请尝试Yii::error('start calculating average revenue'); 它将生成日志文件或添加` `。
  • 或添加'levels' => ['error', 'warning', 'trace'],

标签: yii2


【解决方案1】:

你试试这个。使用类别。比如下图

            'targets' => [
           [
                'class' => 'yii\log\FileTarget',
                'levels' => ['error'],
                 'categories' => ['test1'],
                'logFile' => '@app/Test/test1.log',


            ],

并在控制器动作中使用以下一个

 public function actionIndex(){    
   Yii::error('Test index action', $category = 'test1');   }

【讨论】:

    【解决方案2】:

    尝试在控制台配置中将flushIntervalexportInterval 都设置为1:

    return [
        'bootstrap' => ['log'],
        'components' => [
            'log' => [
                'targets' => [
                    [
                        'class' => 'yii\log\FileTarget',
                        'exportInterval' => 1,
                    ],
                ],
                'flushInterval' => 1,
            ],
        ],
    ];
    

    它使每条日志消息立即出现在日志中。

    【讨论】:

      猜你喜欢
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2017-01-08
      相关资源
      最近更新 更多