zoukankan      html  css  js  c++  java
  • Yii 日志组件

    详细的介绍查看官网的document:http://www.yiiframework.com/doc/guide/1.1/en/topics.logging

    也可以看 Yii 1.1 Application Development Cookbook     这本书很好

    默认的日志是输出到protected/runtime/application.log 

    如果需要修改那么需要在main.php里面增加log配置,如下:

    'components' => array( 
            'log'=>array( 
                'class'=>'CLogRouter', 
                'routes'=>array(           

                     array( 
                         'class'=>'CFileLogRoute', 
                         'levels'=>'trace, info, debug, warn, error, fatal, profile', 
                         'categories'=>'test.*', 
                         'maxFileSize'=>1048576,
                         '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中。

    YII中日志的基本使用:

    可以通过YII提供的Yii::log和Yii::trace进行日志信息的输出

    函数定义

    [php] view plaincopy
     
    1. public static function trace($msg,$category='application')  
    2.      {  
    3.          if(YII_DEBUG)  
    4.              self::log($msg,CLogger::LEVEL_TRACE,$category);  
    5.      }  
    6.      public static function log($msg,$level=CLogger::LEVEL_INFO,$category='application')  
    7.      {  
    8.          if(self::$_logger===null)  
    9.              self::$_logger=new CLogger;  
    10.          if(YII_DEBUG && YII_TRACE_LEVEL>0 && $level!==CLogger::LEVEL_PROFILE)  
    11.          {  
    12.              $traces=debug_backtrace();  
    13.              $count=0;  
    14.              foreach($traces as $trace)  
    15.              {  
    16.                  if(isset($trace['file'],$trace['line']) && strpos($trace['file'],YII_PATH)!==0)  
    17.                  {  
    18.                      $msg.=" in ".$trace['file'].' ('.$trace['line'].')';  
    19.                      if(++$count>=YII_TRACE_LEVEL)  
    20.                          break;  
    21.                  }  
    22.              }  
    23.          }  
    24.          self::$_logger->log($msg,$level,$category);  
    25.      }  

     日志信息的级别:

    [php] view plaincopy
     
    1. const LEVEL_TRACE='trace';用于调试环境,追踪程序执行流程  
    2. const LEVEL_WARNING='warning';警告信息  
    3. const LEVEL_ERROR='error';致命错误信息  
    4. const LEVEL_INFO='info';普通提示信息  
    5. const LEVEL_PROFILE='profile';性能调试信息  


    使用方法:

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


    示例: 
    需先在main.php中进行配置,例子选择将日志存储在文件(系统默认为webappprotected untimeapplication.log)中,为只存储trace和error级别,过滤以orange开始的log。 

    Java代码  收藏代码
    1. 'components'=>array(  
    2.      ...............  
    3.      'log'=>array(  
    4.                'class'=>'CLogRouter',  
    5.                'routes'=>array(  
    6.                     array(  
    7.                          'class'=>'CFileLogRoute',  
    8.                          'levels'=>'trace,error',  
    9.                          'categories'=>'orange.*'  
    10.                     ),  
    11.                ),  
    12.           ),  
    13.      ...............  
    14. )  


      
    在控制器中定义方法并执行,在此为OrangeController控制器 

     
    1. public function actionTest(){  
    2.        Yii::log('This is a  trace log','trace','orange.test');  
    3.    }  


    执行以后可在日志文件中看到我们的trace信息,为 

     
      1. 2012/09/28 15:40:11 [trace] [orange.test] This is a  trace log  
      2. in D:PHPwwwyiiorangeprotectedcontrollersOrangeController.php (24)  
     
  • 相关阅读:
    Struts2 拦截器
    Struts2 常用标签
    Struts2 OGNL表达式、ValueStack
    Struts2 在Action中操作数据
    Struts2 动态方法调用
    Struts2 常量配置
    Struts2 struts.xml配置
    Struts2 Action的3种创建方式
    Struts2 运行流程
    JUnit
  • 原文地址:https://www.cnblogs.com/DaBing0806/p/4638210.html
Copyright © 2011-2022 走看看