Home > Article > Backend Development > Analysis of how to use logs in Yii2 framework
This article mainly introduces the use of logs in the Yii2 framework, and compares the common methods and related usage techniques of Yii1. How to use logs in the framework. Share it with everyone for your reference. The details are as follows:
The difference between Yii2 and Yii1.xHow to use logs in Yii2 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 log method here is the static method of YiiBase.
In Yii2, the object-oriented design is implemented more thoroughly, the logging function is transferred to the Logger class, and multiple output targets (Targets) are supported.
How to use logs in Yii2In order to record logs, you first need to obtain a single instance of the Logger class, and then call the public log of this class Recording method:
Yii::getLogger()->log($msg, $level, $category)
Yii::getLogger()->log(“your site has been hacked”, Logger::LEVEL_ERROR) //默认category为application即应用程序级别的日志
Used to record logs during development and debugging. YII_DEBUG needs to be set to true. Yii::error()
Used to record unrecoverable errors ErrorYii::warning()
Some warning messagesYii::info()
Some system behavior records such as administrator operation prompts
Yii2 customized log output targetTo customize the target, for example, if you want to record files and send emails at the same time when an unrecoverable error occurs, you can customize it as follows:
[ 'bootstrap' => ['log'], // ensure logger gets loaded before application starts 'components' => [ 'log' => [ 'targets' => [ 'file' => [ 'class' => 'yii\log\FileTarget', '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 message', ], ], ], ], ], ]
The above is the detailed content of Analysis of how to use logs in Yii2 framework. For more information, please follow other related articles on the PHP Chinese website!