This article mainly introduces the use of logs in the YII2 framework, compares the common methods of yii1.x analysis YII2 log operations and related use techniques, the need for friends can refer to the following
This article describes how logs are used in the YII2 framework. Share to everyone for your reference, as follows:
The difference between YII2 and yii1.x
Yii2 the use of logs in the same way and Yii 1.x is not the same,
In Yii 1.x, the logging method is
Yii::log ($message, $level, $category); Yii::trace ($message, $category);
The latter logs only in debug mode.
The log method here is a static method of Yiibase.
In Yii2, the object-oriented design is carried out more thoroughly, the logging function is transferred to the logger class, and multiple output targets (Targets) are supported.
How to use logs in Yii2
In order to log, you first need to get the logger class singleton and then call the public logging method of the class:
Yii::getlogger ()->log ($msg, $level, $category)
Examples are as follows:
Yii::getlogger ()->log ("Your site has been hacked", logger::level_error)//default category is application that is the application-level log
In addition, YII2 provides some quick ways to:
Yii::trace()
For logging when developing debugging, you need to set Yii_debug to true.
Yii::error()
Used to log unrecoverable errors
Yii::warning()
Some warning messages
Yii::info()
Some system behavior records such as Administrator action tips
YII2 Custom log Output target
To customize a goal, such as to log files and send messages while an unrecoverable error occurs, you can customize the following:
[' bootstrap ' = [' log '],//ensure logger gets loaded before application starts ' Components ' and [' log ' = ' = ' targets ' + [' file ' + = ' class ' = ' Yii\log\fileta Rget ', ' levels ' = [' trace ', ' info '], ' categories ' = [' yii\* '], ' email ' = [ ' Class ' = ' yii\log\emailtarget ', ' levels ' = [' Error ', ' warning '], ' message ' = [ ' to ' = = [' admin@techbrood.com ', ' support@techbrood.com '], ' subject ' + ' New example.com log Messa GE ',],],], [],],