Explain the relevant configuration of the log in the YII framework of PHP and use _php techniques

Source: Internet
Author: User

The default log is output to Protected/runtime/application.log

If you need to modify it then you need to add the log configuration below the components in main.php, as follows:

' Preload ' => array (' log '),//This sentence must also be added to the ' components ' => array (' Log ' =>array (' class ' => ' Clogrouter '), 
           ' Routes ' =>array (//This is a file route indicates that all types of output category for test will be logged to the runtime/test.log below array ( ' Class ' => ' Cfilelogroute ', ' Levels ' => ' trace, info, debug, warn, error, fatal, Profile ', ' 
         Categories ' => ' test.* ', ' maxfilesize ' =>1048576,//single file Max 1G ' logFile ' => ' Test.log ', ///////development process All logs are exported directly to the browser, so no login server is required to see log the array (' class ' => ' CW Eblogroute ', ' categories ' => ' test.* ', ' Levels ' => clogger::level_profile, ' Showinf Irebug ' => true, ' Ignoreajaxinfirebug ' => true,), array (' class ' => ' CWe  
          Blogroute ', ' categories ' => ' test.* ',), Array (' class ' => ' Cemaillogroute ', ' Levels ' =>'


 Error, warning ', ' emails ' => ' admin@example.com ',),),
If a yii::log is invoked somewhere ("Jdkshgds", "info", ' test.xx ');

This log is first recorded in the memory of a Clogger class array, and then judge each of the Logroute, to determine whether the need for output, attention is to judge, not one of the output next to ignore.

Take the above configuration for example:

The first Cfilelogroute, ' categories ' => ' test.* ', levels contains info, TEST.XX satisfies the condition, so it executes, outputting this log to the Test.log, and then the next cweblogroute, ' Levels ' => clogger::level_profile. And this log is info, so it will not be executed, and then the next Cweblogroute, ' categories ' => ' test.* ', levels unspecified, then say no filtering, so this will also be executed, so this log will be output to the browser.

Second, profile function
In addition logger also has a very powerful function: profile,

Yii::beginprofile (' Blockid ');
... code block being profiled
... Yii::endprofile (' Blockid ');

This will be able to test the code block execution efficiency, very convenient AH.

More detailed configuration view: Http://www.yiiframework.com/doc/api/1.1/CProfileLogRoute

And then there's a very bug feature that Profiling SQL executions

Most of the time the SQL statement is not good enough to affect efficiency, but to determine which one of the statements affect the efficiency of the need to profiling. Yii also provides this bug-level feature.

Iii. use of Yii::log () and Yii::trace ()
first set log in config file

' Log ' =>array ('
  class ' => ' Clogrouter ', '
  routes ' =>array (
    Array (
      ' class ' => ') Cfilelogroute ',//This means that the log output to the file, below the detailed
      ' Levels ' => ' Error, warning ',
    ),
    Array (
      ' class ' => ' Cweblogroute ',//This means that the log is displayed below the page, with detailed
      ' Levels ' => ' trace, info, error, warning ',
      ' categories ' => ' cool . *,system.db.* ',),),



Log Route class:

    • Cdblogroute: Saves the information to a table in the database.
    • Cemaillogroute: Send the message to the specified Email address.
    • Cfilelogroute: Saves information to a file in the Application Runtime directory.
    • Cweblogroute: Displays the information at the bottom of the current page.
    • Cprofilelogroute: Displays overview (profiling) information at the bottom of the page.

Information level levels:

    • Trace: This is the level used in Yii::trace. It is used to track the execution process of a program in development.
    • Info: This is used to record ordinary information.
    • Profile: This is a performance overview (profile). A more detailed description will be available immediately below.
    • Warning: This is used for warning (warning) information.
    • Error: This is for fatal error (fatal error) information.

Classification Categories:

can be customized, but in the output function to be the corresponding will be written into the log
(for example, the above is written cool.* and system.db.*, the corresponding classification of information will be written to the log, please see below to understand)

When you are finished, you can use the Write log function to record:

Yii::trace (' My log message. ', ' cool.pd ');
COOL.PD belong to the cool.* category, so they are written to the log
yii::log (' My log message. ', ' info ', ' cool.collectpd ');
Log definition level is info, combined with the above, the first logclass will be ignored, will not be written to the file, but will be received by the second Logclass, write log is displayed below the network.


The difference between trace () and log ():
Trace () only takes effect in debug mode, when Debug is turned on
Trace () is not level, but log () can set levels parameters


iv. Debugging SQL query the time it takes to execute each statement
under Log in configuration, add the following route

This configuration is dedicated to the database Operations Profile 
Array ( 
    ' class ' => ' Cprofilelogroute ', ' 
    levels ' => clogger::level_profile , 
    ' Showinfirebug ' => true, 
    ' Ignoreajaxinfirebug ' => true, ' categories ' => ' system.db.* ' 
    ,// Log only the operation logs of DB, others are ignored 
,

And then add it to an action in one of the controller:

Yii::beginprofile (' db ', ' Pocketpet '); 
For ($i =0 $i <1000; $i + +) { 
   $user = Usermodel::model ()->findbypk ("1");//Here as long as the database operation on the line, this is just an example 
}

Yii::endprofile (' db ', ' Pocketpet ');

To access this action in the browser, remember to open Firebug first, and then firebug to see the following picture:

The same query will classify, calculate total and average, which is very helpful for analysis.

You can also write DB logs to a file, configured as follows (not recommended, or firebug convenient for browsers):

Array ( 
  ' class ' => ' Cfilelogroute ', ' 
  levels ' => clogger::level_profile, 
  ' categories ' => ') System.db.* ',//only log DB operations, others ignore 
  ' logFile ' => ' Db.log ', 


Of course, there are two steps you can take to get there:

1. Remember to add the following configuration in index.php

$yii = DirName (__file__). ' /.. /yii/framework/yii.php ';
$config = DirName (__file__). ' /protected/config/main.php ';

Defined (' Yii_debug ') or define (' Yii_debug ', true);

Defined (' Yii_debug_show_profiler ') or define (' Yii_debug_show_profiler ', true);
Enable profiling
defined (' yii_debug_profiling ') or define (' yii_debug_profiling ', true);
Trace level
defined (' Yii_trace_level ') or define (' Yii_trace_level ', 3);
Execution time
defined (' Yii_debug_display_time ') or define (' Yii_debug_display_time ', false);
Require_once ($yii);
Yii::createwebapplication ($config)->run ();

2. Set the enableprofiling to true in the components db of the main.php master configuration file

' Components ' => array ('
db ' => array (
    ' enableprofiling ' => true,//This is used to log logs, and records the time of each statement execution
    ') Enableparamlogging ' => true,//true indicates that the information including the parameters of the SQL statement is recorded in the log, very detailed.

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.