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.
'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:
public $output;
public function actionWhatever()
{
$this->stdout("whatever");
}
public function stdout($string)
{
parent::stdout($string);
$this->output = $this->output.$string."\n";
}
public function afterAction($action, $result)
{
$result = parent::afterAction($action, $result);
Yii::trace($this->output, 'categoryName');
return $result;
}
source
share