Thursday, November 25, 2010

Firebug Tutorial – Logging, Profiling and CommandLine (Part I)

Section 1: Console Tab : Logging, Profiling and CommandLine (Part I)

Overview of Console Tab

This tab is mainly used for logging. It also can be used as CommandLine window (like immediate window in Microsoft Visual Studio) while you are debugging the Javascript. It can be used for monitoring the execution of Javascript code by using Profiling service.

The following topic will be covered in this section.

  • Logging in Firebug (with String Substitution pattern )

  • Grouping the logs or messages

  • console.dir and console.dirxml

  • Assertion ( console.assert() )

  • Tracing ( console.trace() )

  • Timing ( Measuring the time of your code)

  • Javascript Profiler (An introduction in this tutorial, the details will be covered in next tutorial.)

#1. Logging in Firebug

Firebug supports logging in Console tab. So, you don’t need to use alert(‘something’) or document.write(‘something’) anymore.

There are five types of logging in Firebug.

  • console.log : Write a message without icon.

  • console.debug : Writes a message to the console, including a hyperlink to the line where it was called

  • erroricon.png console.error() : Writes a message to the console with the visual “error” icon and color coding and a hyperlink to the line where it was called.

  • infoicon.png : Writes a message to the console with the visual “info” icon and color coding and a hyperlink to the line where it was called.

  • warningicon.png console.warn() : Writes a message to the console with the visual “warning” icon and color coding and a hyperlink to the line where it was called.

Example Code:

  • Open the htm file called “Plain HTML” or create one HTML file.

  • Paste the following code with <body> tag.

1   <script language="javascript" type="text/javascript">

2   console.log('This is log message');

3   console.debug('This is debug message');

4   console.error('This is error message');

5'This is info message');

6   console.warn('This is warning message');

7   </script>

You will get the following output. If you click on hyperlink (“test.htm” in this case), it will take you to script tab and will highlight the line that wrote this message.


String Substitution Patterns

String substitution parterns can be used in console.log,, console.debug, console.warn and console.error . You can use the same way that we used in C/C++.

%s        String

%d, %i   Integer (numeric formatting is not yet supported)

%f          Floating point number (numeric formatting is not yet supported)

%o        Object hyperlink

Example :

Note: I will use console.log in the example below even all console objects (console.log,, console.debug, console.warn and console.error ) support string substitution.

  • Remove “script” tag that we pasted for the previous example.

  • Paste the code below within <body> tag.

01 <script language="javascript" type="text/javascript">

03 //This is for normal string substitution " %s, %d, %i, %f".

04 console.log("My Name is <strong>%s</strong>. My Date of Birth is <strong>%dth %s, %i</strong>. My height is <strong>%f</strong> m.", "Nicolas Cage", 7, 'January', 1964, 1.8542);

06 function Foo(){

07 this.LeftHand = function(){

08 return "Left Hand";

09 }

10 this.RightHand = function(){

11 return "Right Hand";

12 }



15 //This is for object "%o".

16 var objFoo = new Foo();

17 console.log('This is <strong>%o</strong> of Foo class.', objFoo);


19 </script>


If you are using %o in your log, the object will be shown as a hyperlink in green color. This hyperlink is linked to the DOM tab. So, If you click “object” in second line, you will see the list of properties of that object (LeftHand and RightHand in this case.)

#2. Grouping

Firebug allows you to group the message or log in Console tab. If you have some many logs in your code, you can probably divide your log into small group or subgroup

Example ~

01   <script language="javascript" type="text/javascript">


03   var groupname = 'group1';

04"message group : %s " , groupname);

05   console.log("log message 1 from %s", groupname);

06   console.log("log message 2 from %s", groupname);

07   console.log("log message 3 from %s", groupname);

08   console.groupEnd();


10   groupname = 'group2';

11"message group : %s " , groupname);

12   console.log("log message 1 from %s", groupname);


14   var subgroupname = 'subgroup1';

15"message group : %s " , subgroupname);

16   console.log("log message 1 from %s", subgroupname);

17   console.log("log message 2 from %s", subgroupname);

18   console.log("log message 3 from %s", subgroupname);

19   console.groupEnd();


21   console.log("log message 3 from %s", groupname);

22   console.groupEnd();


24   </script>


#3. console.dir and console.dirxml

  • console.dir : It can be used for getting all properties and methods of a particular object. According the example below, we can get the Model (property) and getManufactor (method) of Car object by using console.dir(); You can also pass the object of HTML element (eg: console.dir(document.getElementById(‘tbl1′)); ) instead of objCar and let’s see the result. (You will get all properties and methods of the HTML table called “tbl1″).

  • console.dirxml : print the XML source tree of HTML element.

01   <table id="tbl1" cellpadding="0" cellspacing="0" border="0">

02   <tr>

03   <td>A</td>

04   <td>B</td>

05   <td>C</td>

06   </tr>

07   </table>

08   <script language="javascript" type="text/javascript">

09   //Create a class

10   function Car(){

11   this.Model = "Old Model";


13   this.getManufactor = new function(){

14   return "Toyota";

15   }

16   }


18   //Create a object

19   var objCar = new Car();


21   //Firebug

22   console.dir(objCar);

23   console.dirxml(document.getElementById('tbl1'));


25   </script>


#4. Assertion ( console.assert() )

You can use console.assert() to test whether an expression is true or not. If the expression is false, it will write a message to the console and throw an exception.

Example :

01   <script language="javascript" type="text/javascript">

02   function whatIsMyAge(year){

03   var currYear = 2007;

04   return currYear - year;

05   }


07   var yearOfBirth1 = 1982;

08   var age1 = 25;

09   console.assert(whatIsMyAge(yearOfBirth1) == age1);


11   var yearOfBirth2 = 1982;

12   var age2 = 11;

13   console.assert(whatIsMyAge(yearOfBirth2) == age2); //You should get the error here.

14   </script>


#5. Tracing ( console.trace() )

This function is very interesting. Before I tell you the way that I understand, let’s take a look what console.trace does exactly in official website.


Prints an interactive stack trace of JavaScript execution at the point where it is called.

The stack trace details the functions on the stack, as well as the values that were passed as arguments to each function. You can click each function to take you to its source in the Script tab, and click each argument value to inspect it in the DOM or HTML tabs.

This function will tell you about the route information from start point to end point. If you are not clear what I mean, let’s take a look at the sample code and the result.

01   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""">
02   <html xmlns="" >

03   <head>

04   <title>Firebug</title>

05   <script language="javascript" type="text/javascript">

06   function startTrace(str){

07   return method1(100,200);

08   }

09   function method1(arg1,arg2){

10   return method2(arg1 + arg2 + 100);

11   }

12   function method2(arg1){

13   var var1 = arg1 / 100;

14   return method3(var1);

15   }

16   function method3(arg1){

17   console.trace();

18   var total = arg1 * 100;

19   return total;

20   }


22   </script>

23   </head>

24   <body>

25   <input type="button" value="Trace" onclick="startTrace('Result');"/>

26   </body>

27   </html>


Suppose: we wanna know how “method3″ function is invoked. So, we put this code “console.trace()” in that method. then, we run the program and we got the result as picture above. If we read the result from bottom to top, we will see “onclick(click clientX=34, clientY=26)”. That means the execution of Javascript started at on click event of button. then, we got “startTrace(“Result”)” in second line. That means startTrace function is invoked after firing onclick event and the parameter is “Result”. If we keep on checking from bottom to top, we will figure out the completed route from onclick event to method3.

If you wanna test more, you can move this code “console.trace()” to method2(). then, firebug will give the new route from onclick event which is a started point to method2() which is the end point.

I think that it’s pretty useful if you are debugging the other developer’s source code and you have no idea why this function is invoked.

Let me know if you are not clear what I’m trying to explain about console.trace();.

#6. Timing ( Measuring the time of your code)

You can use console.time(timeName) function to measure how long a particular code or function take. This feature is very helpful if you are trying to improve the performance of your Javascript code.

Example :

01   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""">

02   <html xmlns="" >

03   <head>

04   <title>Firebug</title>

05   <script language="javascript" type="text/javascript">

06   function measuretheTime(){

07   var timeName = 'measuringTime';

08   console.time(timeName);


10   for(var i=0;i&lt;1000;i++){

11   ///do something

12   for(var j=0;j&lt;100;j++){

13   //do another thing.

14   }

15   }


17   console.timeEnd(timeName);

18   }

19   </script>

20   </head>

21   <body>

22   <input type="button" value="Trace" onclick="measuretheTime();"/>

23   </body>

24   </html>

Result : measuringTime: 16ms

#7. Javascript Profiler

You can start the profiler thought code (console.profile(‘profileName’)) or by clicking “Profile” button from “Console” tag. It can be used for improving the performance of Javascript. It is similiar to the console.time() function but profiler can give your more advanced and detailed information.

I will tell you about this more details in next tutorial (Part2) . I hope you all are clear about this tutorial.


  1. The training has valued improvement and an additional reputation in the latest time’s recognized Though it is shifted much more within the well-known, the process has been around since the beginning of analysis online.

  2. The blog is good enough I again n again read this.
    List Leverage

  3. Very pleasant and professional office. UX design agency result turned into lovely, it's a bit superbly preserved, and my guy will continually cherish it and the recollections.

  4. Thankfulness to my dad who informed me relating to this blog, this website is really amazing.
    top article

  5. This written piece gives fastidious understanding yet.It’s amazing in support of me to truly have a web site that is valuable meant for my knowledge
    Ministry Of Freedom Review

  6. They integrated well with in-house staff and their sandbox demonstrations persuaded key stakeholders about the viability of the project, making them a valuable partner for the future.
    webdesign firm

  7. Customers can expect a professional niche edit backlinks team with engaged and proficient managers.

  8. The team has provided exceptional leadership, filling all of the client's technical gaps.
    website and logo design company

  9. Wow!! Stunning outcomes from app design studio
    ! I'm never contacting everybody else!

  10. Their attitudes, approach and level of experience were most impressive.
    top branding firms

  11. I even have been getting a lot of helpful and informative material in your web site.
    user interface design firm

  12. Your contents are too straightforward to browse and easy to understand.
    logo creation services

  13. You fully match our expectation and the selection of our data.
    San Francisco web design firms

  14. The complete blogs are really inconceivable and definitely everyone will share this information.
    product design firms