Yii2 console command output

I was wondering how can I save the output of the Yii2 console command to a file? Or how can I register the output so that it can be read later if the command is executed as a cronjob, for example?

Thanks.

Decision

As pointed out by the Beowulfenator, I used the Yii function Logger. So, in my configuration file, I defined a new one FileTargetfor the level trace.

  // config/console.php
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['trace'],
                    'logVars' => [],
                    'logFile' => '@runtime/logs/commands.log'
                ]
            ],
        ],

In my console controller, I redefined the method stdoutas follows:

/* A public variable to catch all the output */
public $output;

/* Example of action outputting something to the console */
public function actionWhatever()
{
     $this->stdout("whatever");
}

/* Overriding stdout, first calling the parent impl which will output to the screen, and then storing the string */
public function stdout($string)
{
    parent::stdout($string);
    $this->output = $this->output.$string."\n";
}

/* In the afterAction hook, I log the output */
public function afterAction($action, $result)
{
    $result = parent::afterAction($action, $result);
    Yii::trace($this->output, 'categoryName');
    return $result;
}
+4
source share
1 answer

- . - , , script:

yii example-controller/example-action > example.log

... - , , :

yii example-controller/example-action >> example.log

yii, .

, . Yii2 . , . , - , Yii::trace() , , echo .

+4

Source: https://habr.com/ru/post/1606031/


All Articles