Yii Framework Analysis Note 10: Log

Source: Internet
Author: User
Tags vars

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
  1. ' Log ' = =Array (
  2. ' Class ' = 'clogrouter ',
  3. ' Routes ' =Array (
  4. Array
  5. ' Class ' = 'cfilelogroute ',
  6. ' Levels ' = 'error, warning ',
  7. ),
  8. Array
  9. ' Class ' = 'cweblogroute ',
  10. ' Levels ' = 'info ',
  11. ' Showinfirebug ' = True
  12. ),
  13. ),
  14. ),

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
  1. /**
  2. * Initializes this application component.
  3. * This method was required by the Iapplicationcomponent interface.
  4. */
  5. Public function init ()
  6. {
  7. Parent::init ();
  8. foreach ($this->_routes as $name =$route)
  9. {
  10. $route =yii::createcomponent ($route);
  11. $route->init ();
  12. $this->_routes[$name]=$route;
  13. }
  14. Yii::getlogger ()->attacheventhandler (' Onflush ',Array ($this,' collectlogs '));
  15. Yii::app ()->attacheventhandler (' onendrequest ',Array ($this,' processlogs '));
  16. }


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
  1. /**
  2. * Logs a message.
  3. * Messages logged by this method is retrieved back via {@link getlogs}.
  4. * @param string $message message to be logged
  5. * @param string $level level of the message (e.g ' Trace ', ' Warning ', ' Error '). It is case-insensitive.
  6. * @param string $category category of the message (e.g ' system.web '). It is case-insensitive.
  7. * @see Getlogs
  8. */
  9. Public function log ($message,$level =' info ',$category =' application ')
  10. {
  11. $this->_logs[]=Array ($message,$level,$category, Microtime (true));
  12. $this->_logcount++;
  13. if ($this->autoflush>0 && $this->_logcount>=$this->autoflush &&! $this->_processing)
  14. {
  15. $this->_processing=true;
  16. $this, flush ($this->autodump);
  17. $this->_processing=false;
  18. }
  19. }

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
  1. /**
  2. * Retrieves filtered log messages from logger for further processing.
  3. * @param CLogger $logger Logger instance
  4. * @param Boolean $processLogs whether to process the logs after they is collected from the logger
  5. */
  6. Public function Collectlogs ($logger, $processLogs =false)
  7. {
  8. $logs =$logger->getlogs ($this->levels,$this->categories);
  9. $this->logs=Empty ($this->logs)?  $logs: array_merge ($this->logs,$logs);
  10. if ($processLogs &&! Empty ($this->logs))
  11. {
  12. if ($this->filter!==null)
  13. Yii::createcomponent ($this->filter)->filter ($this->logs);
  14. $this->processlogs ($this->logs);
  15. $this->logs=Array ();
  16. }
  17. }


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.