转载请注明来自souldak,微博:@evagle

一、基本日志功能
详细的介绍查看官网的document:http://www.yiiframework.com/doc/guide/1.1/en/topics.logging
也可以看
Yii 1.1 Application Development Cookbook  这本书很好

默认的日志是输出到protected/runtime/application.log 
如果需要修改那么需要在main.php里面的
components下面增加log配置,如下:
'preload' => array('log'),//这句也必须加上

'components' => array(


        'log'=>array(


            'class'=>'CLogRouter',


            'routes'=>array(

               //这是一个文件route表示category为test开头的所有类型的输出都会记录到runtime/test.log下面

                 array(

                     'class'=>'CFileLogRoute',

                     'levels'=>'trace, info, debug, warn, error, fatal, profile',

                     'categories'=>'test.*',

                     'maxFileSize'=>1048576,//单文件最大1G

                     'logFile'=>'test.log',

                 ),

                  //

 

//                开发过程中所有日志直接输出到浏览器了,这样不需要登录服务器看日志了    
                    

                 array(

                    'class' => 'CWebLogRoute',

                    'categories' => 'test.*',

                    'levels' => CLogger::LEVEL_PROFILE,

                    'showInFireBug' => true,

                    'ignoreAjaxInFireBug' => true,

                ),

                array(

                    'class' => 'CWebLogRoute',

                    'categories' => 'test.* ',

                ),

array(

                    'class'=>'CEmailLogRoute',

                    'levels'=>'error, warning',

                    'emails'=>'admin@example.com',

                ),

            ),

        ),

 

    ),


如果在某处调用了Yii::log("jdkshgds","info",'test.xx');
这个log首先被记录在了内存中一个CLogger类的array中,然后会逐一的判断每个LogRoute,判断是否需要输出,注意是逐一判断,不是其中一个输出下一个就不管了。
拿上面的配置来说:
第一个CFileLogRoute,'categories'=>'test.*',levels里包含了info, test.xx满足条件,所以会执行,将这条log输出到test.log中,然后下一个CWebLogRoute, 'levels' => CLogger::LEVEL_PROFILE,。而这条log是info的,所以不会执行,再下一个CWebLogRoute,'categories' => 'test.* ',levels没指定,那就说不过滤,所以这个也会被执行,所以这条log将被输出到浏览器中。

二、profile功能
另外logger还有一个很强大的功能:profile, 
Yii::beginProfile('blockID');
...code block being profiled...
Yii::endProfile('blockID');
这样就能测试这个code block的执行效率了,非常的方便啊。
更详细的配置查看:http://www.yiiframework.com/doc/api/1.1/CProfileLogRoute
然后还有一个很BUG的功能,Profiling SQL Executions
很多时候sql语句写的不好会非常影响效率的,但是要确定哪一条语句影响了效率就需要profiling了。YII也提供了这个bug级别的功能。具体参看http://www.yiiframework.com/doc/api/1.1/CDbConnection#enableProfiling-detail
下面是截取的片段:

Profiling is especially useful when working with database since SQL executions are often the main performance bottleneck of an application. While we can manually insert beginProfile and endProfile statements at appropriate places to measure the time spent in each SQL execution, Yii provides a more systematic approach to solve this problem.

By setting CDbConnection::enableProfiling to be true in the application configuration, every SQL statement being executed will be profiled. The results can be readily displayed using the aforementioned CProfileLogRoute, which can show us how much time is spent in executing what SQL statement. We can also call CDbConnection::getStats() to retrieve the total number SQL statements executed and their total execution time.


三、BUG级功能:调试sql query每个语句执行的耗时
在配置中的log下加上下面这个Route
//这个配置专门负责数据库操作的profile

array(

       'class'=>'CProfileLogRoute',

       'levels' => CLogger::LEVEL_PROFILE,

       'showInFireBug' => true,

       'ignoreAjaxInFireBug' => true,

       'categories' => 'system.db.* ', //只记录db的操作日志,其他的忽略

),
然后在某个controller的某个action中加入:
Yii::beginProfile('db', 'pocketpet');

for($i=0;$i<1000;$i++){

      $user = UserModel::model()->findByPk("1");//这里只要是数据库操作就行,这个只是个例子

}
Yii::endProfile('db', 'pocketpet');
在浏览器中访问这个action,记得先打开firebug,然后firebug中就能看到如下图的记录:



相同的query会进行归类,计算total和average,这个对于分析还是非常有帮助的。
也可以将db的日志写到文件,配置如下(不建议使用,还是到浏览器用firebug方便):
array(

    'class'=>'CFileLogRoute',

    'levels' => CLogger::LEVEL_PROFILE,

    'categories' => 'system.db.* ', //只记录db的操作日志,其他的忽略

    'logFile'=>'db.log',

),


当然,想要生效还得有下面两步配置:
1. 记得在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. 在main.php主配置文件里面,的components db 里将enableProfiling设置为true

'components' => array(
'db' => array(
'enableProfiling' => true, //这个是用来记录日志的,会记录每一条语句执行的时间
'enableParamLogging' => true,//true表示包括sql语句的参数在内的信息都会记录到日志里,非常详细
),
)
四、调用示例
Yii::log("dfdf",CLogger::LEVEL_INFO,"example");

Yii::log("dfdf",CLogger::LEVEL_INFO,"example");

Yii::log("dfdf",CLogger::LEVEL_ERROR,"example");

Yii::trace("dfdf", "example");

Yii::trace('example trace message', 'example');

Yii::log('info', CLogger::LEVEL_INFO, 'example');

Yii::log('error', CLogger::LEVEL_ERROR, 'example');

Yii::log('trace', CLogger::LEVEL_TRACE, 'example');

Yii::log('warning', CLogger::LEVEL_WARNING, 'example');

Yii::beginProfile('db', 'example');

for($i=0;$i<1000;$i++){

    $user = UserModel::model()->findByPk("1");

}

Yii::endProfile('db', 'example');

echo 'done';





干货:yii日志功能详解的更多相关文章

  1. 关于syslog日志功能详解 事件日志分析、EventLog Analyzer

    关于syslog日志功能详解 事件日志分析.EventLog Analyzer 一.日志管理 保障网络安全 Windows系统日志分析 Syslog日志分析 应用程序日志分析 Windows终端服务器 ...

  2. MySQL日志功能详解

    MySQL日志功能详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查询日志 它是用来保存所有跟查询相关的日志,这种日志类型默认是关闭状态的,因为MySQL的用户有很多,如果 ...

  3. nginx 日志功能详解

    nginx 日志功能 在 nginx 中有两种日志: access_log:访问日志,通过访问日志可以获取用户的IP.请求处理的时间.浏览器信息等 error_log:错误日志,记录了访问出错的信息, ...

  4. MySQL 日志功能详解

    MySQL日志分类 1:查询日志 :query log     2:慢查询日志:slow_query_log 查询执行时长超过指定时长的查询操作所记录日志     3:错误日志:error log   ...

  5. SVN功能详解

    SVN功能详解   TortoiseSVN是windows下其中一个非常优秀的SVN客户端工具.通过使用它,我们可以可视化的管理我们的版本库.不过由于它只是一个客户端,所以它不能对版本库进行权限管理. ...

  6. UIViewController中各方法调用顺序及功能详解

    UIViewController中各方法调用顺序及功能详解 UIViewController中loadView, viewDidLoad, viewWillUnload, viewDidUnload, ...

  7. Fiddler抓取https请求 & Fiddler抓包工具常用功能详解

    Fiddler抓取https请求 & Fiddler抓包工具常用功能详解   先来看一个小故事: 小T在测试APP时,打开某个页面展示异常,于是就跑到客户端开发小A那里说:“你这个页面做的有问 ...

  8. syslog之一:Linux syslog日志系统详解

    目录: <syslog之一:Linux syslog日志系统详解> <syslog之二:syslog协议及rsyslog服务全解析> <syslog之三:建立Window ...

  9. Java中日志组件详解

    avalon-logkit Java中日志组件详解 lanhy 发布于 2020-9-1 11:35 224浏览 0收藏 作为开发人员,我相信您对日志记录工具并不陌生. Java还具有功能强大且功能强 ...

随机推荐

  1. hibernate学习笔记--可选的配置属性

    3.4.  可选的配置属性 有大量属性能用来控制Hibernate在运行期的行为. 它们都是可选的, 并拥有适当的默认值. 警告: 其中一些属性是"系统级(system-level)的&qu ...

  2. 【BZOJ】【3473】字符串

    后缀数组 Orz zyf 神题不会做啊,先坑着吧……sigh //BZOJ 3473 #include<vector> #include<cstdio> #include< ...

  3. GS玩家登录

    玩家上线 这个过程看了很多很多次了,这里在看下 客户端打开,服务器收到libevent事件,然后new Channel这个过程都付给他各种指针,然后放到channel容器中 .客户端发送c2s_log ...

  4. JavaScript原生折叠扩展收缩菜单带缓冲动画

    JavaScript原生折叠扩展收缩菜单带缓冲动画 @落雨 <div id="div_two" style="display: none;"> &l ...

  5. 使用命令行编译、打包、运行WordCount--不用eclipse

    1)首先创建WordCount1023文件夹,然后在此目录下使用编辑器,例如vim编写WordCount源文件,并保存为WordCount.java文件 /** * Licensed under th ...

  6. PHP 使用 Redis

    PHP 使用 Redis 安装 开始在 PHP 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 PHP redis 驱动,且你的机器上能正常使用 PHP. 接下来让我们安装 PH ...

  7. 【二】php常用方法

    -------------------------------数据类型------------------------------------------ 1.settype(var,type)  类 ...

  8. Leetcode: strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. CAS登录后回传除了ticket参数以外的其他自定义参数

    在一次项目的技术选型中,选择了easyui+cas+shiro+spring的组合,cas实现了单点登录,这使得在一个应用中嵌入另一个应用的页面来展示数据所涉及到的授权方面变得简单. 由于shiro在 ...

  10. HDU 1016 Prime Ring Problem (素数筛+DFS)

    题目链接 题意 : 就是把n个数安排在环上,要求每两个相邻的数之和一定是素数,第一个数一定是1.输出所有可能的排列. 思路 : 先打个素数表.然后循环去搜..... #include <cstd ...