2015-09-05 3 views
3

Мне было интересно, как я могу сохранить вывод команды консоли Yii2 в файл? Или как я могу регистрировать вывод, чтобы его можно было прочитать позже, если команда выполняется, например, как cronjob?Выход команды консоли Yii2

Спасибо.

РЕШЕНИЕ

Как отметил Beowulfenator, я использовал Logger особенность Yii в. Итак, в моем файле конфигурации я определил новый FileTarget для уровня 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' 
       ] 
      ], 
     ], 

В мой контроллер консоли, я переопределен в stdout метод, как это:

/* 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; 
} 

ответ

Смежные вопросы