The logging component in the YII Framework records Class 5 classes, which have been defined by constants in Clogger:
Const level_trace= ' TRACE ';
Const level_warning= ' WARNING ';
Const level_error= ' ERROR ';
Const level_info= ' INFO ';
Const level_profile= ' profile ';
Clogger provides an interface for all log writes and fetches, and distributes the logs to different log presentation or storage media through the log routing management class Clogrouter.
Log Component Configuration
[PHP]View Plaincopy
- ' Log ' = =Array (
- ' Class ' = 'clogrouter ',
- ' Routes ' =Array (
- Array
- ' Class ' = 'cfilelogroute ',
- ' Levels ' = 'error, warning ',
- ),
- Array
- ' Class ' = 'cweblogroute ',
- ' Levels ' = 'info ',
- ' Showinfirebug ' = True
- ),
- ),
- ),
Log Route Initialization
When the log component is created, each log route in the configuration is initialized with Clogrouter::init (), and a refresh and request end event is added, which is a great help to the performance improvement of Yii logs. Yii default in the case of the number of logs in a single request less than 1000, the log data is placed in memory, when more than 1000 to execute the Onflush event, the log is flushed to the receiving media, when the request is over to execute onendrequest and then refresh the log, Doing so reduces the number of IO operations.
[PHP]View Plaincopy
- /**
- * Initializes this application component.
- * This method was required by the Iapplicationcomponent interface.
- */
- Public function init ()
- {
- Parent::init ();
- foreach ($this->_routes as $name =$route)
- {
- $route =yii::createcomponent ($route);
- $route->init ();
- $this->_routes[$name]=$route;
- }
- Yii::getlogger ()->attacheventhandler (' Onflush ',Array ($this,' collectlogs '));
- Yii::app ()->attacheventhandler (' onendrequest ',Array ($this,' processlogs '));
- }
Log calls
The operation of the log is through the Yii::log () static method to implement the call to Clogger, the following is the Clogger log method, the log is saved in $this->_logs[], when the refresh event is triggered, the flushing process is performed.
[PHP]View Plaincopy
- /**
- * Logs a message.
- * Messages logged by this method is retrieved back via {@link getlogs}.
- * @param string $message message to be logged
- * @param string $level level of the message (e.g ' Trace ', ' Warning ', ' Error '). It is case-insensitive.
- * @param string $category category of the message (e.g ' system.web '). It is case-insensitive.
- * @see Getlogs
- */
- Public function log ($message,$level =' info ',$category =' application ')
- {
- $this->_logs[]=Array ($message,$level,$category, Microtime (true));
- $this->_logcount++;
- if ($this->autoflush>0 && $this->_logcount>=$this->autoflush &&! $this->_processing)
- {
- $this->_processing=true;
- $this, flush ($this->autodump);
- $this->_processing=false;
- }
- }
To trigger the Onflush event, log routing Clogrouter::collectlogs ($event) passes the log object to the Collectlogs () of each log, and then through the Processlogs () To handle the presentation of different log levels.
[PHP]View Plaincopy
- /**
- * Retrieves filtered log messages from logger for further processing.
- * @param CLogger $logger Logger instance
- * @param Boolean $processLogs whether to process the logs after they is collected from the logger
- */
- Public function Collectlogs ($logger, $processLogs =false)
- {
- $logs =$logger->getlogs ($this->levels,$this->categories);
- $this->logs=Empty ($this->logs)? $logs: array_merge ($this->logs,$logs);
- if ($processLogs &&! Empty ($this->logs))
- {
- if ($this->filter!==null)
- Yii::createcomponent ($this->filter)->filter ($this->logs);
- $this->processlogs ($this->logs);
- $this->logs=Array ();
- }
- }
Instance
Yii::log ("Test", clogger::level_info);
Yii::log ("Test2", clogger::level_info);
The following are the effects of cweblogroute displaying logs in the Web and in the Chrome console, respectively. (The default time differs from Beijing time by 8 hours)
Yii Framework Analysis Note 10: Log