Log not working in yii2

半城伤御伤魂 提交于 2020-01-05 03:49:26

问题


i want to put a log in app.log ,My config file

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

in controller action

 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,
        ]);

} But Yii::trace('start calculating average revenue'); not working


回答1:


You try this.Use categories. For example like below

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


            ],

And use below one in controller action

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



回答2:


Try to set both flushInterval and exportInterval to 1 in console config:

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

It makes each log message appearing immediately in logs.



来源:https://stackoverflow.com/questions/39871779/log-not-working-in-yii2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!