Zend Framework and Firebug – Log and Debug your Projects

When developing an application there are some important factors that you have to pay close attention to avoid problems in the future. I think one of the most important is logging information on how your application is working and when it fails.

Must of us like to log only big exceptions and fatal errors, but the truth is, that when you are in the development process it’s very important to keep track of not just errors, but important information of when something gets executed. As a web developer one of the most important tools to have isFireBug . If you didn’t know, FireBug has an API that you can use to send console messages for logging purposes, when debugging JavaScript. But, did you know you can use FireBug to debug your php applications?

Through  Wildfire , a project that was created to help developers have a standard way for sending messages between different programming languages and scripts. The  Zend Framework   is now able to communicate to the Firebug console in order to display logging messages injected by PHP.

Using this component is pretty straight forward if you are using Zend_Controller_Front (MVC) with zend Framework.

Place this in your bootstrap file before dispatching your front controller

1 $writer   =  new   Zend_Log_Writer_Firebug();
2 $logger   =  new   Zend_Log( $writer );

Then whenever you want to log something to Firebug just use it in your model, view, or controller like this.

1 $logger ->log( 'This is an INFORMATIONAL log message!' , Zend_Log::INFO);
2 $logger ->log( 'This is a WARNING log message!' , Zend_Log::WARN);
3 $logger ->log( 'This is an ERROR log message!' , Zend_Log::ERROR);

If you notice in the example above, you can specify the type of message you want to log (INFO, WARN, etc..). This is helpful when you want to clearly identify what type of message you are viewing since it will apply a special formatting and in some instances provide more information.

If you want to use the  Zend_Log_Writer_Firebug  as a stand alone here is the sample code you can use:

01 //Instantiate the Firebug Writer
02 $writer   =  new   Zend_Log_Writer_Firebug();
03 $logger   =  new   Zend_Log( $writer );
04  
05 //start the wildfire component
06 $request   =  new   Zend_Controller_Request_Http();
07 $response   =  new   Zend_Controller_Response_Http();
08 $channel   = Zend_Wildfire_Channel_HttpHeaders::getInstance();
09 $channel ->setRequest( $request );
10 $channel ->setResponse( $response );
11  
12 // Start output buffering
13 ob_start();
14  
15 // Now you can make calls to the logger
16  
17 $logger ->log( 'This is a log message!' , Zend_Log::INFO);
18  
19 // Flush log data to browser
20 $channel -> flush ();
21 $response ->sendHeaders();

If you don’t use ZendFramework there is another alternative to php developers. FirePhp, will also enable you to send log messages to Firebug by using php method calls. Be sure to check  FirePhp website   to get more information.

So remember that this is just one easy and great way to debug your projects, there are many other tools and methods you can incorporate into your projects. If you want to share other ways or simply just want to ask a question regarding this post, feel free to leave a comment.

http://www.smooka.com/blog/2009/08/21/zend-framework-firebug-log-debug/

Setting up logging with Firebug and FirePHP in Zend Framework 1.7.5

 

Question: How do I set up logging with FirePHP in Zend Framework?

Answer:

  • Step 1: Make sure you have Zend Framework 1.6+
  • Step 2: Install both Firebug and FirePHP extensions for Firefox.
  • Step 3: Make sure you have Console and Net enabled./
  • Step 4: Set up Zend Log in your bootstrap file:
1 $writer   =  new   Zend_Log_Writer_Firebug();
2 $logger   =  new   Zend_Log( $writer );

You can now send a log message to Firebug with the following code (in your bootstrap file):

1 $logger ->log( 'Hello World!' , Zend_Log::DEBUG);

If you wanted to use the logger outside of your bootstrap file, you can set $logger in the session:

1 Zend_Registry::set( 'logger' ,  $logger );
2  
3 $logger   = Zend_Registry::get( 'logger' );
4 $logger ->log( 'This is my log message' , Zend_Log::INFO);

However, when I want to debug, I don’t want a bunch of lines or characters when I just want to output something to the logger. So I prefer to make a function in my bootstrap file that can be used throughout my application:

1 $writer   =  new   Zend_Log_Writer_Firebug();
2 $logger   =  new   Zend_Log( $writer );
3 Zend_Registry::set( 'logger' ,  $logger );
4  
5 function   debug( $message ) {
6 $logger   = Zend_Registry::get( 'logger' );
7 $logger ->debug( $message );
8 }

That way, in my I can use by simply calling:

 

1 debug( 'hello!' );

 

 

 

http://blog.andrewhavens.com/2009/02/26/setting-up-logging-with-firephpfirebug-in-zend-framework-175/

 

 

 

 

 

 

 

 

http://www.phpfans.net/ask/question4/3315199848.html

 

Zend framework: Logging Database Queries to FireBug》(个人翻译)

本帖最后由 yoursjzz 于 2010-1-11 21:58 编辑 

现在才知道,越想越气,越想越感觉自己真是个杯具,原来查询语句可以在ZF中完整输出的,一气之下翻译了下面文章。
PS:没有任何用翻译工具,纯手工翻译(其实原文也很简单的说)......因此若有翻译不对的地方,大家多多包涵,也请大家指出翻译不好的地方


----------------------------分界线---------------------------------------------


Logging database queries to FireBug is sinfully simple with the newcomponent Zend_Db_Profiler_Firebug in ZF 1.6, now available, you candownload it here   Zend Framework Download Page . // 使用ZF1.6新的组件:Zend_Db_Profiler_Firebug 可以很方便地把数据查询语句输出到FIREBUG中,通过以上链接你可以下载最新版本的ZF。(后半句完全个人意译


Requirements: //需求
  • Firefox Browser ideally version 3 but version 2 is also supported.//需要FF3,当然FF2也可以支持

  • Firebug Firefox Extension.//Firebug 插件

  • FirePHP Firefox Extension.//FirePHP 插件


More information on requirements at the   Zend Framework Documentation -  Profiling with Firebug //更多信息,请到文中的链接查询
Let’s look at some examples. //看看一些例子
<   ?php // Instatiate the database //初始化数据库
$db   =   Zend_Db :: factory ( 'Pdo_Mysql' ,       array (      
      'host'   =>   'localhost' ,      
    'dbname'   =>   'zf_feature_testing' ,      
    'username'   =>   'user123' ,      
    'password'   =>   'pass123'       ) ) ;
// Instantiate the profiler in your bootstrap file  //在你的bootstrap中实例化profiler 
$profiler   =   new   Zend_Db_Profiler_Firebug ( 'All Database Queries:' ) ; // Enable it
$profiler -> setEnabled ( true ) ; // Attach the profiler to your db adapter 
$db -> setProfiler ( $profiler ) ;   // Run your queries
$result1   =   $db -> fetchAll ( 'SELECT * FROM zf_test' ) ;
$result2   =   $db -> fetchAll ( 'SELECT * FROM zf_test where id = ?' ,   3 ) ;


Alternatively you can add the profiler parameters to the Zend_Db factory. //可选择的是,你可以把 profiler 的参数增加到 Zend_Db 工厂
<   ?php // Instatiate the database, passing in the profiler parameters
.
$db   =   Zend_Db :: factory ( 'Pdo_Mysql' ,   
       array (          'host'   =>   'localhost' ,    
                        'dbname'   =>   'zf_feature_testing' ,    
                         'username'   =>   'user123' ,      
                            'password'   =>   'pass123' ,    
                           'profiler'   =>   array (         
                                              'enabled'   =>   true ,         
                                              'class'   =>   'Zend_Db_Profiler_Firebug'      
      )       ) ) ;


Or from an .ini file using Zend_Config_Ini//或者,通过Zend_Config_Ini增加到.ini文件中
$config   =   new   Zend_Config_Ini ( '../application/config.ini' ,   'development' ) ;
$db   =   Zend_Db :: factory ( $config -> database ) ;


config.ini
[development]
database.adapter                     =   pdo_mysql
database.params.host                  =   localhost
database.params.username             =   user123
database.params.password             =   pass123
database.params.dbname               =   zf_feature_testing
database.params.profiler.enabled      =   true
database.params.profiler.class       =   Zend_Db_Profiler_Firebug


Show me my profiling data?
Open FireBug, you will see a link under console. //打开你的FIREBUG,你将会看到在控制台中有一个链接

Click it open and it will list all the queries that were run. //打开该链接,将会看到所有的查询语句

Enjoy!
----------------------------分界线---------------------------------------------
排版不好,也请大家见谅了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值