Home  >  Article  >  Backend Development  >  Yii Framework Official Tutorial Supplement 45 - Special Topic: Logging

Yii Framework Official Tutorial Supplement 45 - Special Topic: Logging

黄舟
黄舟Original
2017-02-16 09:34:001632browse



Yii provides a flexible and scalable logging function. Recorded logs can be classified by log level and information classification. By using level and category filters, selected information can be further routed to different destinations, such as a file, email, browser window, etc.

1. Information recording

Information can be recorded through Yii::log or Yii::trace. The difference is that the latter only logs information 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 category and level. The category is a format similar to a path alias. String. For example, if a message 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 flow of a program during development.

  • info: This is used to record ordinary information.

  • Yii Framework Official Tutorial Supplement 45 - Special Topic: Logging: This is the performance overview (Yii Framework Official Tutorial Supplement 45 - Special Topic: Logging). More detailed instructions will follow shortly.

  • warning: This is used for warning information.

  • error: This is used for fatal error messages.

2. Information routing

The information recorded through Yii::log or Yii::trace is stored in memory. We usually need to display them in a browser window, or save them to some persistent storage such as a file or email. This is called information routing, for example, sending information to different destinations.

In Yii, information routing is managed by an application component called CLogRouter. It is responsible for managing a series of things called log routing. Each log route represents a separate log destination. Messages sent through a log route are filtered by their level and category.

To use information routing, we need to install and preload a CLogRouter application component. We also need to configure its routes attribute for the log routes we want. The code below demonstrates an example of the required application configuration:


array(
    ......
    'preload'=>array('log'),
    'components'=>array(
        ......
        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array(
                    'class'=>'CFileLogRoute',
                    'levels'=>'trace, info',
                    'categories'=>'system.*',
                ),
                array(
                    'class'=>'CEmailLogRoute',
                    'levels'=>'error, warning',
                    'emails'=>[email protected]',
                ),
            ),
        ),
    ),
)

In the above example, we define Two log routes. The first is CFileLogRoute, which saves the information in a file located in the application's runtime directory. And only information with level trace or info and categories starting with system. will be saved. The second route is CEmailLogRoute, which will send information to the specified email address, and only the level of error or warning will be sent.

In Yii, the following log routes are available:

  • CDbLogRoute: Save information to a database table.

  • CEmailLogRoute: Send information to the specified Email address.

  • CFileLogRoute: Saves information to a file in the application's runtime directory.

  • CWebLogRoute: Display information at the bottom of the current page.

  • CProfileLogRoute: Displays profiling information at the bottom of the page.

Information: Information routing occurs when the last onEndRequest event of the current request cycle is triggered. To explicitly terminate the current request process, please call CApplication::end() instead of using die() or exit(), because CApplication::end() will trigger the onEndRequest event so that the information will be recorded smoothly.

3. Message filtering

As we mentioned, messages can be filtered by their level and category before they are sent to a log router. This is done by setting the levels and categories attributes of the corresponding log route. Multiple levels or categories should be connected with commas.

Since the information classification is similar to xxx.yyy.zzz format, we can regard it as a classification level. Specifically, we say that xxx is the parent of xxx.yyy, which in turn is the parent of xxx.yyy.zzz. In this way, we can use xxx.* to represent category xxx and all its child and grandchild categories.

4. Record context information

Starting from version 1.0.6, we can set up to record additional context information, such as PHP predefined variables (such as $_GET, $_SERVER), session ID , username, etc. This is accomplished by specifying a suitable log filtering rule in the CLogRoute::filter attribute of a log route.

框架使用非常方面的可以用于大多数日志过滤的CLogFilter,默认情况下, CLogFilter 将会记录一条包含变量(如通常包含系统上下文变量值的如$_GET,$_SERVER等)的信息。CLogFilter还可以用于配置到每一个日志信息之前,带上session ID,用户名等。当我们要查找日志信息的位置时这将带来极大的便利 。

下面的配置展示了如何开启记录上下文信息。每一个日志路由都有自己的日志过滤器,但是默认情况下, 日志路由并不包含日志过滤器。


array(
    ......
    'preload'=>array('log'),
    'components'=>array(
        ......
        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array(
                    'class'=>'CFileLogRoute',
                    'levels'=>'error',
                    'filter'=>'CLogFilter',
                ),
                ...other log routes...
            ),
        ),
    ),
)

从版本1.0.7开始,Yii支持记录通过调用Yii::trace返回的日志记录信息中的回调栈信息。这一特性默认是取消的,因为这回降低性能。想要使用这一特性,只需在入口脚本中定义一个名为YII_TRACE_LEVEL的大于0的常量 (在包含yii.php之前),然后Yii将会在每一个trace信息之后加上应用代码回调栈的文件名和行号。 YII_TRACE_LEVEL 决定了每一个回调栈的层级将会被记录。这个信息在开发期间很有用,因为这可以帮助我们确定触发trace信息的位置。

5. 性能分析

性能分析是一个特殊的日志记录类型。性能分析可以用于衡量指定代码块的运行时间,并且找出性能瓶颈。

使用性能分析,我们需要指定被分析的代码块。我们通过插入如下代码来标记每一个代码块的开始和结束:


Yii::beginProfile('blockID');
...code block being Yii Framework Official Tutorial Supplement 45 - Special Topic: Loggingd...
Yii::endProfile('blockID');

其中blockID 指的是代码块的唯一标志符.

注意, 代码块需要被合理嵌套。也就是说,一个代码块不能和另一代码块交叉嵌套:要么是并行的,要么是完全封闭包含在另一个代码块里。

为了显示分析结果, 需要安装 一个包含CProfileLogRoute日志路由的CLogRouter 应用组件。这和我们处理其他的信息路由一样,CProfileLogRoute路由将会在当前页面的底部显示性能分析结果。

Yii Framework Official Tutorial Supplement 45 - Special Topic: Logging

6. SQL执行分析

性能分析在处理数据库操作时尤为有用,因为 SQL 执行经常是一个应用主要的性能瓶颈。 我们当然可以在每一次SQL执行的地方插入beginProfile 和 endProfile语句, 从版本1.0.6开始, 但Yii 提供了一个更加系统的方式来解决这个问题。

通过在应用配置中设置 CDbConnection::enableProfiling 为true, 每一个被执行的SQL语句都会被分析. 结果可以通过设置前面提到的CProfileLogRoute来显示, 这样我们就能知晓每一个SQL语句的执行时间。除此之外我们还可以调用CDbConnection::getStats() 来取回SQL语句执行的次数和总的执行时间。

 以上就是Yii框架官方教程增补版45——专题:日志记录的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn