Error-Free Console Logging

var Fb = {}; //An empty object literal for holding the function
Fb.log = function(obj, consoleMethod) {
       if (window.console && window.console.firebug && window.console.firebug.replace(/^\s\s*/, '').replace(/\s\s*$/, '') !== '') {
               if (typeof consoleMethod === "string" && typeof console[consoleMethod] === "function") {
               } else {

If you leave console.log,, etc messages in your JavaScript code and open the page in browser like IE then it may halt the page loading completely saying that 'console not defined' especially if your user uses a non IE8 browser.

This code snippet will allow you to leave the logging message as such in your code if you wish and your page will render properly in IE or any other browser that does not support the console messages.


Fb.log("This will be logged");

Fb.log("This will be displayed in console as info", "info");

The FB.log function accepts two parameters the first one is the "item" that you want to display in the firebug console and the second one is the firebug method that you want to use for the logging, like info, error, etc. If you omit the second parameter the result will be equivalent to console.log()

Simple log-only way

function ltc(what) {
       try {
       catch (e) {}
       finally {


  1. User Avatar
    Benjamin Mayo
    Permalink to comment#

    Or (at the top of your code) you could just overwrite console.log() entirely if it doesn’t exist with an empty function.

    This removes the evaluation of the condition on every call, and keeps the function call uniform to other applications. New developers to the project will immediately realize what console.log() is for, unlike possible confusion that may arise from Fb.log().

    • User Avatar
      Permalink to comment#

      Hi Benjamin,

      1. The approach you’ve mentioned will work if someone uses only console.log from within the JS.

      The coders may use every logging functions supported by Firebug like info, error, etc

      In such a situation this may be difficult.

      2. I have used a custom object literal just because this is a custom function and thought of avoiding tinkering with Firebug’s console object.

  2. User Avatar
    Erik Lundmark
    Permalink to comment#


    I missed rownumbers though, so I modified this a little and added that, together with “support” for console.count & console.time|timeEnd

    To show an error, you’d do this, where 55 is your files row number.

    Q.log(55, "something went wrong!!", "error");

    And a simple “info” would be this:

    Q.log(77, $("#mySelector"), "info");

    And to count ( ref: ) write this:

    Q.log(99, "count", "info");

    To time events you only need 2 parameters, a unique number/string and the words “time” & “timeEnd”.

    Q.log(1, "time");

    So to stop this timer you use:

    Q.log(1, "timeEnd");
    /* A fork of */
    // Q.log(84, "count", "info"); -> initial number would your rownumber, which is the whole idea here
    // Q.log(77, $("#div"), "info");
    // Q.log(99, "something happened!!!", "error");
    // Q.log(1, "time"); -> Q.log(1, "timeEnd"); -> 1 can also be a string, but keep it equal and unique
    var Q = {};
    Q.log = function( rowNum, obj, consoleMethod ) {
        if ( window.console && window.console.firebug && window.console.firebug.replace(/^\s\s*/, '').replace(/\s\s*$/, '' ) !== '') {
            if ( typeof consoleMethod === "string" && typeof console[consoleMethod] === "function" ) {
                if ( typeof obj === "string" && obj === "count" ) {
                    console.count("Row: " + rowNum + " | Counter ");
                } else {
                    console[consoleMethod]("Row: " + rowNum + " ", obj);
            } else if ( typeof obj === "string" && obj === "time" ) {
                console.time("Profile " + rowNum);
            } else if ( typeof obj === "string" && obj === "timeEnd" ) {
                console.timeEnd("Profile " + rowNum);
            } else {
  3. User Avatar
    Erik Lundmark
    Permalink to comment#

    Sorry for the multiple comments, but I thought this could come to some use…

    If all you want is to avoid the error thrown in Firefox, this should work just fine:

    var hasConsole = window.console != undefined ? true : false;
    ifConsole &&"Console Info Test");
        First, declare ifConsole -> is it present?
        Then prepend your console.[function] line with 'ifConsole && '
  4. User Avatar
    Simon Davies
    Permalink to comment#

    I was wondering if there is a better way in which to handel this or is this code still good today?

    • User Avatar
      Permalink to comment#

      window.console && console.log(“ see my code in the comment below”);

  5. User Avatar
    Permalink to comment#

    There is a way to keep it low, it uses javascript’s features to safely wrap console.loggings
    –> window.console && console.log(“This single line execution won’t disappoint you !!!”);

  6. User Avatar
    Paulo Batista
    Permalink to comment#

    Be careful: the window.console.firebug was removed at firebug 1.9.0 (See in and

  7. User Avatar
    Permalink to comment#

    I have recently created a console.log script that mimics the browser console. So no need for Firebug Lite.

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.