頭部引入log類別
use yiilogFileTarget;
$time = microtime(true);
$log = new FileTarget();
$log->logFile = Yii::$app->getRuntimePath() . '/ logs/zhidemy.com.log'; //檔名自訂
$log->messages[] = ['test',1,'application',$time];
$log->export();
這樣基本上就能寫出來了,先看一下$log->message的類別描述訊息
/*
* [0] => message (mixed, can be a string or some complex data, such as an exception object)
* [1] => level (integer)
* [2] => category (string)
* [3] => timestamp (float, obtained by microtime(true))
* [4] => traces (array, debug backtrace, contains the application code call stacks)
*/
傳遞參數依照這些資訊傳遞就可以了。最後記住重要的一點
$log->messages[] 別忘了加[]
具體可查看Target類
以上就介紹了Yii2 自訂日誌檔案寫日誌,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。