logVars in config

I’m trying to get $_POST included in the logVars with this code:

$config = [

'components' => [


    'log' => [


        'traceLevel' => YII_DEBUG ? 2 : 0,


        'targets' => [


            [


                'class' => 'yii\log\FileTarget',


                'levels' => ['error', 'warning'],


                'logVars' => ['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION'],


            ],

When I implemented this I saw $_SERVER disappear but $_POST doesn’t appear.

This is a bit annoying as we are running a JSON API via XHR.

OK, it’s dawned on me why this doesn’t work. My stupidity again (see other posts by me).

Our XHR calls don’t use POST. The protocol is POST but we don’t define any POST variables.

Instead we send a string like this:

{"tableName":"properties","page":1,"filters":{"properties.dbClientNo":12103},"sort":{"dbPropertyName":"ASC"}}

and in Yii we read it like this:

$this->json = Json::decode(file_get_contents("php://input"), false);

See http://php.net/manual/en/wrappers.php.php

As this isn’t available in $GLOBALS Yii2 won’t log it for us. :(

We use the Yii log file a lot, e.g. when an error is reported in production as well as when developing.

Fixed this by overriding getContextMessage as per http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html


namespace app\components;


use yii;


class cbdFileLogger extends \yii\log\FileTarget {

    protected function getContextMessage(){

        $output = "\n\n" . file_get_contents("php://input");

        $output .="\n\n" . parent::getContextMessage();

        return $output;

    }

}

and in web.php


'log' => [

            'traceLevel' => YII_DEBUG ? 2 : 0,

            'targets' => [

                [

                    'class' => 'app\components\cbdFileLogger',

                    'levels' => ['error', 'warning'],

                    'logVars' => ['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION'],

                ],