257

According to this post it was in the beta, but it's not in the release?

1
  • 65
    console.log is there in IE8, but the console object isn't created until you open DevTools. Therefore, a call to console.log may result in an error, for example if it occurs on page load before you have a chance to open the dev tools. The winning answer here explains it in more detail.
    – SDC
    Commented Jan 28, 2013 at 15:31

17 Answers 17

259

console.log is only available after you have opened the Developer Tools (F12 to toggle it open and closed). Funny thing is that after you've opened it, you can close it, then still post to it via console.log calls, and those will be seen when you reopen it. I'm thinking that is a bug of sorts, and may be fixed, but we shall see.

I'll probably just use something like this:

function trace(s) {
  if ('console' in self && 'log' in console) console.log(s)
  // the line below you might want to comment out, so it dies silent
  // but nice for seeing when the console is available or not.
  else alert(s)
}

and even simpler:

function trace(s) {
  try { console.log(s) } catch (e) { alert(s) }
}
11
  • 11
    Either way you shouldn't be calling console.log blindly because $other-browsers mightn't have it and thus die with a JavaScript error. +1 Commented Mar 27, 2009 at 17:05
  • 8
    you'll probably want to turn off traces before release anyhow though ;) Commented Mar 27, 2009 at 17:06
  • 2
    It makes sense not to log without developer tools being open, but making it throw an exception if rather than failing silently is the real confusing decision here.
    – ehdv
    Commented Aug 3, 2010 at 20:50
  • 4
    I want to point out a downside to wrapping console.log like this... you won't see anymore where your logging is coming from. I find that very useful sometimes on top of which is just looks wrong to have every console line originating from the exact same location in your code. Commented Aug 31, 2011 at 11:36
  • 2
    alert is evil. Some code behaves differently when alerts are used because the document loses focus, making bugs even harder to diagnose or creating ones where there weren't before. Also, if you accidentally leave a console.log in your production code, it is benign (assuming it doesn't blow up) - just silently logs to the console. If you accidentally leave an alert in your production code, the user experience is ruined. Commented Apr 2, 2014 at 13:21
230

Even better for fallback is this:


   var alertFallback = true;
   if (typeof console === "undefined" || typeof console.log === "undefined") {
     console = {};
     if (alertFallback) {
         console.log = function(msg) {
              alert(msg);
         };
     } else {
         console.log = function() {};
     }
   }

4
  • 71
    This is so impractical - how could you possibly debug a website with something that throws an alert for every call to console.log(). What if you have 10+ calls to log() in your code. What if msg is an object? Walter's answer makes much more sense, as a starting point.
    – Precastic
    Commented Jun 22, 2013 at 10:47
  • 7
    @Precastic: People will just stop using the browser :P Commented Mar 30, 2014 at 4:44
  • See my comment on Mister Lucky's answer. Commented Apr 2, 2014 at 13:22
  • 1
    an unobtrusive (though imperfect) alternative fallback is to set document.title. At least it doesn't lock up the browser with a modal alert. Commented Oct 1, 2015 at 12:27
58

This is my take on the various answers. I wanted to actually see the logged messages, even if I did not have the IE console open when they were fired, so I push them into a console.messages array that I create. I also added a function console.dump() to facilitate viewing the whole log. console.clear() will empty the message queue.

This solutions also "handles" the other Console methods (which I believe all originate from the Firebug Console API)

Finally, this solution is in the form of an IIFE, so it does not pollute the global scope. The fallback function argument is defined at the bottom of the code.

I just drop it in my master JS file which is included on every page, and forget about it.

(function (fallback) {    

    fallback = fallback || function () { };

    // function to trap most of the console functions from the FireBug Console API. 
    var trap = function () {
        // create an Array from the arguments Object           
        var args = Array.prototype.slice.call(arguments);
        // console.raw captures the raw args, without converting toString
        console.raw.push(args);
        var message = args.join(' ');
        console.messages.push(message);
        fallback(message);
    };

    // redefine console
    if (typeof console === 'undefined') {
        console = {
            messages: [],
            raw: [],
            dump: function() { return console.messages.join('\n'); },
            log: trap,
            debug: trap,
            info: trap,
            warn: trap,
            error: trap,
            assert: trap,
            clear: function() { 
                  console.messages.length = 0; 
                  console.raw.length = 0 ;
            },
            dir: trap,
            dirxml: trap,
            trace: trap,
            group: trap,
            groupCollapsed: trap,
            groupEnd: trap,
            time: trap,
            timeEnd: trap,
            timeStamp: trap,
            profile: trap,
            profileEnd: trap,
            count: trap,
            exception: trap,
            table: trap
        };
    }

})(null); // to define a fallback function, replace null with the name of the function (ex: alert)

Some extra info

The line var args = Array.prototype.slice.call(arguments); creates an Array from the arguments Object. This is required because arguments is not really an Array.

trap() is a default handler for any of the API functions. I pass the arguments to message so that you get a log of the arguments that were passed to any API call (not just console.log).

Edit

I added an extra array console.raw that captures the arguments exactly as passed to trap(). I realized that args.join(' ') was converting objects to the string "[object Object]" which may sometimes be undesirable. Thanks bfontaine for the suggestion.

4
  • 4
    +1 This is the only solution that starts to make sense. In what world would you not want to see the messages you are explicitly sending to the console!
    – Precastic
    Commented Jun 22, 2013 at 10:40
  • Great answer. Really liked the IIFE article you've mentioned, probably one of the best i've read so far. Could you please elaborate what is the purpose for these two lines in trap function: var args = Array.prototype.slice.call(arguments); var message = args.join(' '); ? Why do you pass the the arguments through this to the message? Commented Aug 20, 2013 at 13:19
  • 1
    @user1555863 I've updated my answer to answer your questions, see the section below the code. Commented Aug 20, 2013 at 13:46
  • 1
    I think your "console.clear()" function's second line should read "console.raw.length = 0", instead of "console.row.length = 0".
    – Steve J
    Commented Mar 16, 2014 at 13:26
52

It's worth noting that console.log in IE8 isn't a true Javascript function. It doesn't support the apply or call methods.

3
  • 3
    +1 This is my precise error this morning. I'm trying to apply arguments to console.log and IE8 is hating me. Commented Mar 15, 2012 at 9:49
  • [joke] Microsoft says "it's unsafe for us to let us people overwrite console object" :/
    – tomasdev
    Commented May 9, 2012 at 5:52
  • 1
    I've been using: console.log=Function.prototype.bind.call(console.log,console); to get around this.
    – mowwwalker
    Commented Jan 12, 2013 at 1:06
44

Assuming you don't care about a fallback to alert, here's an even more concise way to workaround Internet Explorer's shortcomings:

var console=console||{"log":function(){}};
4
  • +1 Since I'm scoping my code out in an anonymous function, placing console into a variable like this is the best solution to me. Helps me to not interfere with any other console hooking going on in other libraries.
    – Codesleuth
    Commented Aug 13, 2012 at 9:43
  • 2
    You want to start logging as soon as the developer tools have been opened. If you put this solution in a long-lived scope (e.g. registers inner functions as callbacks), it'll keep using the silent fallback. Commented Sep 11, 2013 at 4:35
  • +1/-1 = 0: +1 because the solution should be more based around preventing console.logs from breaking a site in IE - not used to debug... If you want to debug, just hit f12 and open the console :) -1 because you should be checking if console exists before overwriting it.
    – 1nfiniti
    Commented Oct 10, 2013 at 18:55
  • Some IE plugins define console and console.log, but as empty objects, not functions. Commented Oct 15, 2013 at 18:39
24

I really like the approach posted by "orange80". It's elegant because you can set it once and forget it.

The other approaches require you to do something different (call something other than plain console.log() every time), which is just asking for trouble… I know that I'd eventually forget.

I've taken it a step further, by wrapping the code in a utility function that you can call once at the beginning of your javascript, anywhere as long as it's before any logging. (I'm installing this in my company's event data router product. It will help simplify the cross-browser design of its new admin interface.)

/**
 * Call once at beginning to ensure your app can safely call console.log() and
 * console.dir(), even on browsers that don't support it.  You may not get useful
 * logging on those browers, but at least you won't generate errors.
 * 
 * @param  alertFallback - if 'true', all logs become alerts, if necessary. 
 *   (not usually suitable for production)
 */
function fixConsole(alertFallback)
{    
    if (typeof console === "undefined")
    {
        console = {}; // define it if it doesn't exist already
    }
    if (typeof console.log === "undefined") 
    {
        if (alertFallback) { console.log = function(msg) { alert(msg); }; } 
        else { console.log = function() {}; }
    }
    if (typeof console.dir === "undefined") 
    {
        if (alertFallback) 
        { 
            // THIS COULD BE IMPROVED… maybe list all the object properties?
            console.dir = function(obj) { alert("DIR: "+obj); }; 
        }
        else { console.dir = function() {}; }
    }
}
2
  • 1
    Glad you like it :-) I use it for the exact reason you mention--b/c it's a good safety. It is just way too easy to put some "console.log" statements in your code for development and the forget to remove them later. At least if you do this, and put it at the top of every file where you use console.log, you will never have the site breaking in customers' browsers b/c they fail on console.log. Saved me before! Nice improvements, btw :-)
    – jpswain
    Commented Dec 8, 2011 at 7:03
  • 1
    "It is just way too easy to ... forget to remove them". One helpful thing I always do with temporary debug logging is prefix the code with an empty comment, /**/console.log("..."); so it's easy to search and locate the temporary code. Commented Dec 5, 2013 at 23:19
8

If you get "undefined" to all of your console.log calls, that probably means you still have an old firebuglite loaded (firebug.js). It will override all the valid functions of IE8's console.log even though they do exist. This is what happened to me anyway.

Check for other code overriding the console object.

0
6

The best solution for any browser that lack a console is:

// Avoid `console` errors in browsers that lack a console.
(function() {
    var method;
    var noop = function () {};
    var methods = [
        'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
        'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
        'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
        'timeStamp', 'trace', 'warn'
    ];
    var length = methods.length;
    var console = (window.console = window.console || {});

    while (length--) {
        method = methods[length];

        // Only stub undefined methods.
        if (!console[method]) {
            console[method] = noop;
        }
    }
}());
1
  • 1
    This has the glaring problem that objects or strings logged using console.group or console.GroupCollapsed will completely disappear. This is unnecessary, they should be mapped to console.log, if it's available.
    – Ben
    Commented Aug 19, 2013 at 23:18
4

There are so many Answers. My solution for this was:

globalNamespace.globalArray = new Array();
if (typeof console === "undefined" || typeof console.log === "undefined") {
    console = {};
    console.log = function(message) {globalNamespace.globalArray.push(message)};   
}

In short, if console.log doesn't exists (or in this case, isn't opened) then store the log in a global namespace Array. This way, you're not pestered with millions of alerts and you can still view your logs with the developer console opened or closed.

3

Here is my "IE please don't crash"

typeof console=="undefined"&&(console={});typeof console.log=="undefined"&&(console.log=function(){});
2
if (window.console && 'function' === typeof window.console.log) {
    window.console.log(o);
}
2
  • Are you saying that window.console.log() might be available in IE8 even when console.log() isn't?
    – LarsH
    Commented Jan 7, 2013 at 18:58
  • The problem here is that typeof window.console.log === "object", not "function" Commented Apr 5, 2013 at 15:04
2

I found this on github:

// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function f() {
    log.history = log.history || [];
    log.history.push(arguments);
    if (this.console) {
        var args = arguments,
            newarr;
        args.callee = args.callee.caller;
        newarr = [].slice.call(args);
        if (typeof console.log === 'object') log.apply.call(console.log, console, newarr);
        else console.log.apply(console, newarr);
    }
};

// make it safe to use console.log always
(function(a) {
    function b() {}
    for (var c = "assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","), d; !! (d = c.pop());) {
        a[d] = a[d] || b;
    }
})(function() {
    try {
        console.log();
        return window.console;
    } catch(a) {
        return (window.console = {});
    }
} ());
2

I'm using Walter's approach from above (see: https://stackoverflow.com/a/14246240/3076102)

I mix in a solution I found here https://stackoverflow.com/a/7967670 to properly show Objects.

This means the trap function becomes:

function trap(){
    if(debugging){
        // create an Array from the arguments Object           
        var args = Array.prototype.slice.call(arguments);
        // console.raw captures the raw args, without converting toString
        console.raw.push(args);
        var index;
        for (index = 0; index < args.length; ++index) {
            //fix for objects
            if(typeof args[index] === 'object'){ 
                args[index] = JSON.stringify(args[index],null,'\t').replace(/\n/g,'<br>').replace(/\t/g,'&nbsp;&nbsp;&nbsp;');
            }
        }
        var message = args.join(' ');
        console.messages.push(message);
        // instead of a fallback function we use the next few lines to output logs
        // at the bottom of the page with jQuery
        if($){
            if($('#_console_log').length == 0) $('body').append($('<div />').attr('id', '_console_log'));
            $('#_console_log').append(message).append($('<br />'));
        }
    }
} 

I hope this is helpful:-)

1

I like this method (using jquery's doc ready)... it lets you use console even in ie... only catch is that you need to reload the page if you open ie's dev tools after the page loads...

it could be slicker by accounting for all the functions, but I only use log so this is what I do.

//one last double check against stray console.logs
$(document).ready(function (){
    try {
        console.log('testing for console in itcutils');
    } catch (e) {
        window.console = new (function (){ this.log = function (val) {
            //do nothing
        }})();
    }
});
1

Here is a version that will log to the console when the developer tools are open and not when they are closed.

(function(window) {

   var console = {};
   console.log = function() {
      if (window.console && (typeof window.console.log === 'function' || typeof window.console.log === 'object')) {
         window.console.log.apply(window, arguments);
      }
   }

   // Rest of your application here

})(window)
1
  • Good that it is limited in scope, could support the case when IE8 DevTools are open in the middle of code execution, but it doesn't work in IE8, console.log is an object, so it has no apply method.
    – Nishi
    Commented Jun 23, 2016 at 11:24
1

Make your own console in html .... ;-) This can be imprved but you can start with :

if (typeof console == "undefined" || typeof console.log === "undefined") {
    var oDiv=document.createElement("div");
    var attr = document.createAttribute('id'); attr.value = 'html-console';
    oDiv.setAttributeNode(attr);


    var style= document.createAttribute('style');
    style.value = "overflow: auto; color: red; position: fixed; bottom:0; background-color: black; height: 200px; width: 100%; filter: alpha(opacity=80);";
    oDiv.setAttributeNode(style);

    var t = document.createElement("h3");
    var tcontent = document.createTextNode('console');
    t.appendChild(tcontent);
    oDiv.appendChild(t);

    document.body.appendChild(oDiv);
    var htmlConsole = document.getElementById('html-console');
    window.console = {
        log: function(message) {
            var p = document.createElement("p");
            var content = document.createTextNode(message.toString());
            p.appendChild(content);
            htmlConsole.appendChild(p);
        }
    };
}
0

It works in IE8. Open IE8's Developer Tools by hitting F12.

>>console.log('test')
LOG: test
2
  • 6
    This issues "undefined" in my case.
    – acme
    Commented Nov 25, 2010 at 9:52
  • 6
    As Mister Lucky pointed out: "console.log is only available after you have opened the Developer Tools (F12 to toggle it open and closed)." Commented Nov 28, 2011 at 14:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.