Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 always log application category with $_COOKIE, $_SESSION and $_SERVER (category filter not working properly)

Tags:

php

logging

yii2

I am new to Yii2 and I need some manual logging to Data Base after some actions has happened. The thing that seems best for me is to filter by category. The problem is that Yii2 always add extra line with information $_COOKIE, $_SESSION and $_SERVER. Is this normal? How can I disable the extra log line?

This is the fronted configuration

return [
    'id' => 'app-frontend',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'frontend\controllers',
    'components' => [
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\DbTarget',
                    'categories' => ['manual'],
                ]
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
    ],
    'params' => $params,
];

And this is the action code:

public function actionTest()
{
    $logger = Yii::getLogger();
    \Yii::info('catalog info', 'manual');
    $logger->flush();
    Yii::$app->end();
}

And this is the result:

enter image description here

Thanks to rkm answer this configuration now works:

[
    'id' => 'app-frontend',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'frontend\controllers',
    'components' => [
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'except' => [
                        'manual',
                    ],
                    'class' => 'yii\log\FileTarget',
                    'categories' => ['application'],
                ],
                [
                    'class' => 'yii\log\DbTarget',
                    'categories' => ['manual'],
                    'logVars' => [],
                ]

            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
    ],
    'params' => $params,
];
like image 311
Natan Rubinstein Avatar asked Dec 24 '22 14:12

Natan Rubinstein


1 Answers

Add 'logVars' => [], in your config to log component like this if you don't need any global variables.

'components' => [
    ...
    'log' => [
        ...  
        'targets' => [
            [
                'class' => 'yii\log\FileTarget',
                'logVars' => [],
            ]
        ]
        ...
    ]
...
]

More info about configuring logging in the docs

like image 161
rkm Avatar answered Dec 27 '22 20:12

rkm