PHP development framework YiiFramework tutorial (3) add logs to applications

Source: Internet
Author: User
The log storage location and file name can be modified through configuration. Generally, you can use the default value. similar to log4X, application logs can be written to multiple destinations at the same time through routing (files, for more information, see logging.

In the process of application development, debugging is also a very important part, in addition to the real-time debugging supported by the IDE (such as. PHP or IDE-supported debugging functions), it is also very useful to add appropriate debugging information for Web applications, developed Java or. net applications are no stranger to log4XX, and Yii Framework also provides similar Log functions, Yii: log, which appears as a built-in component of CWebApplication. You can configure it through the configuration file (the configuration in Yii is usually protected/config/main. php ).

In the previous article, the simple Yii Framework Development Tutorial (2) Yii Web application basic briefly describes the application components.

In addition to the Log component, Yii predefines a series of core application components to provide functions used in common Web applications. For example, the request component is used to parse user requests and provide information such as URLs and cookies. By configuring the attributes of these core components, we can modify the default behavior of Yii in almost all aspects.

The core components predefined by CWebApplication are listed below.

AssetManager: CAssetManager-manage the publishing of private resource files.

AuthManager: CAuthManager-manage role-based access control (RBAC ).

Cache: CCache-provides the data cache function. Note: You must specify the actual class (such as CMemCache and CDbCache ). Otherwise, NULL is returned when you access this component.

ClientScript: CClientScript-manages client scripts (javascripts and CSS ).

CoreMessages: CPhpMessageSource-provides the translation of core information used by the Yii Framework.

Db: CDbConnection-provides database connection. Note: To use this component, you must configure its connectionString attribute.

ErrorHandler: CErrorHandler-handle uncaptured PHP errors and exceptions.

Format: CFormatter-format the value display. This function is available from version 1.1.0.

Messages: CPhpMessageSource-provides information translation for Yii applications.

Request: CHttpRequest-provides information about user requests.

SecurityManager: CSecurityManager-provides security-related services, such as hash and encryption.

Session: CHttpSession-provides session-related functions.

StatePersister: CStatePersister-provides a global state persistence method.

UrlManager: CUrlManager-provides URL parsing and creation functions

User: CWebUser-provides the current user's identification information.

Themanager: CThemeManager-manage topics. These components will be gradually introduced in subsequent tutorials. The following provides the basic method for using the Log function: Here we modify the Yii Framework development concise tutorial (1) The first application Hello World. Add logs for it. 1. create the configuration file protected/config/main. php 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 is routed through the Yii: log or Yii: trace record information is stored in the memory. We usually need to display them in a browser window, or save them to some persistent storage such as files and emails. This is called information routing.

When using the Log function, you need to pre-load the Log component, which is configured through preload. preload allows the application to load some modules in advance. tip: by default, application components are 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 many components are configured. Some application components (such as CLogRouter) need to be created no matter they are used. In this case, the ID column of these components is preload in the application configuration file.

2. modify the entry script index. php and configure the master application instance to use the newly created configuration file.

$ Yii = 'C:/yiiframework/yii. php ';
// Remove the following line when in production mode
Defined ('II _ debug') or define ('II _ debug', true );

$ Config = dirname (_ FILE _). '/protected/config/main. php ';

Require_once ($ yii );
Yii: createWebApplication ($ config)-> run (); 3. create protected/runtime

Because the default directory for writing log files is protected/runtime, you must create the runtime directory in advance. otherwise, Yii will directly display the log on the webpage.

4. you can use Yii: log or Yii: trace to add logs to the application.

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

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

Yii: trace ($ message, $ category );

When recording information, we need to specify its classification and level classification as a string in a format similar to the 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 during development.

Info: this is used to record common information.

Profile: This is the Performance Overview (profile ). More details will be provided below.

Warning: this is used for warning information.

Error: This message is used for fatal error messages.

Yii logs are classified and filtered by Level and Category. As we mentioned, multiple levels or categories should be connected by commas.

Because information classification is similar to xxx. yyy. zzz, we can regard it as a classification level. Specifically, xxx is the parent level of xxx. yyy, and xxx. yyy is the parent level of xxx. yyy. zzz. In this way, we can use xxx. * to indicate the classification of xxx and all its sublevels and grandchildren.

With the above knowledge, we modify the actionIndex of SiteController and add a line of log.

Public function actionIndex ()
{
Yii: log ("action", "info", "site. action ");
$ This-> render ("index ");
} 5. run the application. you can see that Yii created an application. log in protected/runtime.

The content is as follows:

21:23:38 [info] [site. action] action

Note: The log storage location and file name can be modified through configuration. Generally, you can use the default value, which is similar to log4X, application logs can be written to multiple destinations (files, emails, etc.) through routing at the same time. for details, see log records.


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.