PHP Development Framework YII Framework Tutorial (3) Adding logs to your app

Source: Internet
Author: User
Tags yii
Debugging is also a very important part in the development of the application, in addition to the IDE's support for immediate debugging (such as vs.php or IDE-supported debugging capabilities), to add the appropriate debugging information to the Web application is also a very useful way to develop Java or. Net applications to log4xx are not unfamiliar , the YII framework also provides a similar log function, 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).

Last article Yii Framework Development Concise Tutorial (2) Yii Web application based on the application of a brief description of the component.

In addition to the log component, Yii pre-defines a series of core application components that provide functionality used in common WEB applications. For example, the request component is used to resolve 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 pre-defined 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 (for example, Cmemcache, Cdbcache). Otherwise, NULL will be returned when you access this component.

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

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

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

Errorhandler:cerrorhandler-Handles Uncaught PHP errors and exceptions.

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

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 parsing and creation-related features

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

Thememanager:cthememanager-Manage Topics. These components are introduced in a later tutorial. Here's a basic way to use the log feature: Here we modify the Yii Framework Development Brief Tutorial (1) The first app Hello World. Add a log for 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:

This is the main WEB application configuration. Any writable//cwebapplication properties can be configured Here.return array (//preloading ' log ' component ' preload ' = = Array (' log '),//Application components ' components ' =>array (' Log ' =>array (' class ' = ' clogrouter ', ' routes ' = >array (Array (' class ' = ' Cfilelogroute ', ' levels ' = ' info,error, warning ',),),),);

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

In general, the log function needs to be preloaded with the log component, which is configured via preload, preload allows the application to load some modules beforehand. Tip: By default, the app component is created as needed. This means that a component is created only when it is accessed. As a result, the overall performance of the system is not degraded by 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 list the IDs of these components in the application's configuration file: Preload.

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

$yii = ' c:/yiiframework/yii.php ';//Remove the following line while in production modedefined (' 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 must be created beforehand, or yii will display the log directly on the Web page.

4. This allows you to add logs to your app using Yii::log or Yii::trace.

The difference is that the latter only logs information when the application is running in debug mode (debug modes).

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

When logging information, we need to specify its classification and level classification as a string formatted like a path alias. For example, if a piece of information is recorded in Ccontroller, we can use System.web.CController as the classification. The information level should be one of the following values:

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 common information.

Profile: This is a performance overview (profile). A more detailed description is now available.

Warning: This is used for warning (warning) information.

Error: This is used for fatal errors (fatal error) information.

Yii logs are categorized and filtered by level and category, and as we mentioned, multiple levels or classifications should be connected using commas.

Since information classification is similar to XXX.YYY.ZZZ format, we can consider it as a classification level. Specifically, we say that XXX is the parent of xxx.yyy, and Xxx.yyy is the parent of xxx.yyy.zzz. This allows us to use xxx.* to represent the classification of XXX and all its children and grandchildren.

With the above knowledge, we modify Sitecontroller's actionindex and add a row of logs.

Public Function Actionindex () {Yii::log ("action", "info", "site.action"), $this->render ("index");}

5. To run the application, you can see that Yii created a application.log in Protected/runtime

The contents are as follows:

2012/12/11 21:23:38 [INFO] [site.action] Action

Note: The location of the log and the file name can be modified by the configuration, the general use of the default value can be, and similar to log4x, the application of the log can be written to multiple destinations (file, email, etc.) can be implemented through the configuration, in particular, see the log records.

The above is the PHP Development Framework Yii Framework Tutorial (3) for the application to add the content of the log, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.