PHP Development Framework YII Framework Tutorial (3) Add log for application

Source: Internet
Author: User
Tags config yii

Debugging is also a very important part of developing the application, in addition to the IDE-supported Just-in-time debugging (such as vs.php or IDE-supported debugging), it is also a useful way to add appropriate debugging information to Web applications, and it is no stranger to log4xx that have developed Java or. Net applications. , the YII framework also provides a similar log feature, Yii::log, which appears as a built-in component of cwebapplication. Configuration files can be configured (typically, protected/config/main.php is configured in Yii).

A concise tutorial on Yii Framework Development (2) YII Web application base describes the application components.

In addition to the log component, Yii predefined a series of core application components that provide the functionality used in common WEB applications. For example, the request component is used to parse user requests and provide information such as Url,cookie. By configuring the properties of these core components, we can modify the default behavior of Yii in almost all respects.

Below we list the core components that are predefined by cwebapplication.

Assetmanager:cassetmanager-manages the publication of private resource files.

Authmanager:cauthmanager-Manage role-based access control (RBAC).

Cache:ccache-Provides data caching capabilities. Note that you must specify the actual class (e.g. Cmemcache, Cdbcache). Otherwise, you will return NULL when you access this component.

Clientscript:cclientscript-Manages client script (JavaScripts and CSS).

Coremessages:cphpmessagesource-Provides a translation of the core information used by the YII framework.

Db:cdbconnection-Provides a database connection. Note that you must configure its connectionString properties with this component.

Errorhandler:cerrorhandler-Handles not caught PHP errors and exceptions.

Format:cformatter-format numeric display. This feature is available from version 1.1.0.

Messages:cphpmessagesource-provides translation of information used in YII applications.

Request:chttprequest-Provides information about the user's request.

Securitymanager:csecuritymanager-Provides security-related services, such as hashing, encryption.

Session:chttpsession-provides session-related functionality.

Statepersister:cstatepersister-Provides a global state persistence method.

Urlmanager:curlmanager-provides URL resolution and creation-related functionality

User:cwebuser-Provides identification information for the current user.

Thememanager:cthememanager-Manage Topics. These components are described step-by-step in a later tutorial. The following is a basic method for using the log function: Here we modify the Yii Framework Development Concise Tutorial (1) The first application Hello world. Add a log to it. 1. Create a configuration file protected/config/main.php you want to write the log to a file, you can use the following configuration:

<?php     
//This is the main WEB application configuration. Any writable     
//Cwebapplication properties can is configured here.     
Return Array (     

///preloading ' log ' component '     
preload ' =>array (' log '),     

//Application Components     
' Components ' =>array ('     

        log ' =>array ('     
            class ' => ' Clogrouter ',     
            ' routes     
                ' =>array "(     
                    ' class ' => ' Cfilelogroute ', '     
                    levels ' => ' info,error, warning ',     
                    )     
            ,     
        ),     

);

Clogrouter information that is routed through Yii::log or Yii::trace records is stored in memory. We usually need to display them in a browser window or save them to some persistent storage such as files, emails. This is called information routing.

In general, the log feature needs to be preloaded with the log component, which is configured by preload, preload allows applications to be loaded with some modules. Tip: By default, the application component is created as needed. This means that a component is created only when it is accessed. Therefore, the overall performance of the system will not be reduced because of the configuration of many components. Some application components, such as Clogrouter, are created regardless of whether they are used or not. In this case, we will include the ID column of these components in the applied configuration file: preload.

2. Modify the portal script index.php to configure the main application instance to use the newly created configuration file.

$yii = ' c:/yiiframework/yii.php ';     
Remove the following line production mode     
defined (' Yii_debug ') or define (' Yii_debug ', true);     
         
$config =dirname (__file__). ' /protected/config/main.php ';     
         
Require_once ($yii);     
Yii::createwebapplication ($config)->run ();

3. Create Protected/runtime

Because the default directory written by the log file is Protected/runtime, the runtime directory is created beforehand, or yii displays the log directly on the Web page.

4. This allows you to add logs for the application using Yii::log or Yii::trace.

The difference is that the latter only records information when the application is running in debug mode.

Yii::log ($message, $level, $category);

Yii::trace ($message, $category);

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.