Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Logging Information to the Firebug JavaScript Console

9 Responses »

Firebug is the ultimate Web Developer debugging tool. Firebug gives you control over the page's XHTML, JavaScript, CSS, AJAX requests, and more. It's important not to simply take in what Firebug tells you though -- you can log information to your Firebug console from within your page's JavaScript. Here are a few of the helpful methods you can use:

console.log()

console.log('Application is starting.');

The console.log() method logs a message to the console without providing a line number. Simple but useful.

console.debug()

console.debug('Gets to this point without error.');

The console.debug() method logs a message to Firebug console just like log(), but debug() provides a line number reference.

console.info()

console.info('DOM is ready, now executing Moo event.');

The console.info() method logs a message to the Firebug console with a blue "information" icon. Line number reference is included.

console.warn()

console.warn('Form field [title] has no value.');

console.warn() sets a warning within the console. The warning provides a yellow background color to easily spot warnings within the console.

console.error()

console.error('The [title] element is undefined.  Bad news.');

The console.error() method places a custom error message within the console with a pink background. Easy to spot.

Each of the above methods accept an "object", or array of objects, and works much like a print-f.

With Objects

console.log("The %d item has a value of: %d", fifth, myvalue);

This probably looks familiar to a PHP programmer.

Make the most of your Firebug -- send your own message to the console!

Discussion

  1. sean o
    July 25, 2008 @ 8:26 am

    Using Firebug’s console() can be quite helpful. Just remember to remove calls to it before going to production as, surprisingly, IE will kick out JS errors aplenty. Or remember to use firebugx.js.

  2. braxo
    July 25, 2008 @ 11:33 am

    When removing the console calls I do a simple find and replace of ‘console.’ with ‘//console.’

    Plus, I know console.log can be used the same way in Safari’s developer window.

  3. July 25, 2008 @ 1:37 pm

    Thanks for the tips! I know that you can mess with Firebug a little with this:

    http://davidwalsh.name/how-to-sniff-firebug-disable

  4. July 26, 2008 @ 4:01 am

    Thanks for posting. I only knew of console.log untill now.

  5. anurag phadke
    August 11, 2008 @ 11:24 am

    Is there a way to output the “Net” log to a file on my local machine? i.e. I want to output to a file all the URLs of images/swfs/text etc. that get loaded when a page is requested by the user.

  6. August 11, 2008 @ 12:18 pm

    @anurag: Good question — I couldn’t find anything.

  7. david
    June 5, 2009 @ 10:06 am

    Ensuring that your console.xxx() calls don’t throw errors when the code runs outside of a Firebug environment was always a concern of mine. The four options for handling it that I have seen are:

    Remove the comnsole.xxx() calls before publishing, as noted by the first two commenters above.
    if (!window.console){ window.console.log = function {}; }
    if (!window.console){ window.console.log = function { // real implementation here }; }
    window.console && console.log(‘My message to the console.’);

    Lately, I confess I’ve been doing a combination of (1) and (4). (1) keeps the code as lean as possible, but (4) allows me to miss a few without risking an error.

    [ BTW: Dave, I'm new to your blog, but just can't say enough positive about it. The content, the look, the style. Just right freaking on. You might even have me sold on MooTools, as well. My investment in jQuery on a few projects is probably just at the level where I could conceivably back out and go with MooTools. But that's a whole different discussion for another time. Anyway, thanks and kudos on the site. ]

  8. February 14, 2010 @ 11:06 pm

    David, just like the david before me (wow, this is getting confusing), your blog is awesome! It has taught me alot about mootools and javascript in general. Anyways, i decided to stop being a nub and use firefox’s console to log things and i came across your blog. I liked it so much I wrote an entry combining a couple of your entries onto my blog. Keep up the good work!

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!