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:
02 | $writer = new Zend_Log_Writer_Firebug(); |
03 | $logger = new Zend_Log( $writer ); |
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 ); |
17 | $logger ->log( 'This is a log message!' , Zend_Log::INFO); |
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/
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 ); |
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 ); |
5 | function debug( $message ) { |
6 | $logger = Zend_Registry::get( 'logger' ); |
7 | $logger ->debug( $message ); |
That way, in my I can use by simply calling:
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》(个人翻译)
现在才知道,越想越气,越想越感觉自己真是个杯具,原来查询语句可以在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: //需求