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 latter only records logs in
debug modestaticObject-orienteddesign is implemented more thoroughly, the logging function is transferred to the Logger class, and supports multiple output targets (Targets).
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)
Examples are as follows:
Yii::getLogger()->log(“your site has been hacked”, Logger::LEVEL_ERROR) //默认category为application即应用程序级别的日志
In addition, Yii2 provides some shortcut methods:
Yii::trace() for development and debugging When recording logs, you need to set YII_DEBUG to true.Yii::error()
Used to record unrecoverable errorsYii::warning()
Some warnings InformationYii::info()
Some system behaviors
records such as administrator operation prompts
Yii2 customized log output target To 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 PHP example-analysis of how to use logs in Yii2 framework. For more information, please follow other related articles on the PHP Chinese website!