Home > Article > Backend Development > Yii2 custom log file writing log
Introduce the log class in the header
use yiilogFileTarget;
$time = microtime(true);
$log = new FileTarget();
$log->logFile = Yii::$app->getRuntimePath(). '/logs/zhidemy.com.log'; //Customized file name
$log->messages[] = ['test',1,'application',$time];
$log->export( );
This can basically be written. Let’s first look at the class description information of $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)
*/
Just pass the parameters according to this information. Finally, remember an important point
$log->messages[] Don’t forget to add []
For details, please view the Target class
The above introduces the Yii2 custom log file writing log, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.