Yii provides a highly customizable and extensible frame which facilitates to easily log various types of messages.
To log a message, you should call one of the following methods −
The above methods record log messages at various categories. They share the following function signature −
A simple and convenient way of naming scheme is using the PHP __METHOD__ magic constant. For instance −
A log target is an instance of the yii\log\Target class. It filters all log messages by categories and exports them to file, database, and/or email.
Step 1 − You can register multiple log target as well, like.
By the above code, two targets are registered. All errors, warnings, traces, and info messages are selected and saved in a database by the first target. The second target sends all error and warning messages to the admin email.
Yii provides the following built-in log targets −
By default, log messages are formatted as follows −
Step 2 − Configure the yii\log\Target::$prefix property to customize the format. For instance,
The above code snippet configures a log target to prefix all log messages with the current userID.
By default, log messages include the values from these global PHP variables: $_GET, $_POST, $_SESSION, $_COOKIE, $_FILES, and $_SERVER. This behaviour is modified by configuring the yii\log\Target::$logVars property with the names of variables that are desired to be include.
All log messages are maintained in an array by the logger object. The logger object flushed the recorded messages to the log targets each time the array accumulates a certain number of messages(default is 1000).
Step 3 − This number is customized by calling the flushInterval property.
Even when the logger object flushes log messages to log targets, they do not get exported immediately. The export occurs when a log target accumulates a certain number of messages(default is 1000).
Step 4 – This number is customized by configuring the exportInterval property.
Step 5 − The config/web.php file is modified by the code:
By the above code, a log application component is defined and the flushInterval and exportInteval properties are set to 1 so that log messages appear in the log files immediately. Also omit the levels property of the log target. It means that log messages of all categories(error, warning, info, trace) will appear in the log files.
Step 6 − A function actionLog() is created in the SiteController.
In the above code, four log messages of different categories are written to the log files.
Step 7 – Enter the URL http://localhost:8080/index.php?r=site/log in the address bar of the web browser. Log messages should appear under the app/runtime/logs directory in the app.log file.
Yii Related Interview Questions
|PHP Interview Questions||Zend Interview Questions|
|PHP and Jquery Interview Questions||PHP+MySQL Interview Questions|
|Sybase Interview Questions||Dbase Interview Questions|
|Magento Interview Questions||PHP5 Interview Questions|
|WordPress Interview Questions||CakePHP Interview Questions|
|CodeIgniter Interview Questions||Firebase Interview Questions|
|SQLite Interview Questions||PHP7 Interview Questions|
|Object Oriented Programming in PHP Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.