13

I'm trying to use console.log to put some logging into the javascript side of my program. I noticed, though, that unless the dev console is open in IE, JS basically stops working when it hits console.log. This is a pain... it means I have to remove all the logging whenever I want to do a production build.

Aside from the obvious:

function DoSafeConsoleLog( parameters )
{
    if ( !$.browser.msie )
    {
         console.log( parameters ); 
    }
}

is there a good way to log javascript that is friendly to all major browsers?

EDIT:

Well, after looking at the duplicate post (oops) as well as considering the answers here, I've gotta side with just checking for the existence of console before calling. Even though I am loathe to have the extra markup, I would rather not step on the feet of future programmers who might want to use Firebug Lite to debug my code.

Sean Anderson
  • 27,963
  • 30
  • 126
  • 237
  • possible duplicate of [Testing for console.log statements in IE](http://stackoverflow.com/questions/7585351/testing-for-console-log-statements-in-ie) – zzzzBov Oct 25 '11 at 16:39
  • possible duplicate of ['console' is undefined error for internet explorer](http://stackoverflow.com/questions/3326650/console-is-undefined-error-for-internet-explorer) – skolima Oct 26 '11 at 13:21

6 Answers6

15

You can create a fake console:

if (typeof console === "undefined")
    console = { log: function() { } };
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • I'm not particularly fond of this idea as it would override consoles added after page load such as [Firebug lite](http://getfirebug.com/firebuglite). – zzzzBov Oct 25 '11 at 16:38
  • Honestly I fought with firebug lite a lot and ended up removing it. I found that Chrome's developer console was enough for me to figure things out on -- and Firebug lite would glitch out probably once a day and have to be closed. Just my experiences, though. – Sean Anderson Oct 25 '11 at 16:42
  • 1
    @Sean Anderson, `` will add it only for IE. – zzzzBov Oct 25 '11 at 16:48
9

IE has its own console, and you wont want to override console if you're using firebug lite. Just make sure that console exists when log gets called:

if (window.console) console.log('foo bar baz', fizz, buzz);

Better yet, use && to shortcut:

window.console && console.log('foo bar baz', fizz, buzz);
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • 1
    What do you mean "IE has its own console?" The console it provides works happily with console.log IF the developer console is open. The other browsers are able to log to their console even if it is not explicitly open. Just curious, not saying your answer is incorrect at all. You definitely have valid points I am considering. :) – Sean Anderson Oct 25 '11 at 16:44
  • I switched to `window.console && console.log()` and now everything works in IE! – Avishai Aug 18 '15 at 22:59
  • Just a heads-up, it is mostly a bad idea to let `console.log`s seep into your production code. – Sunny R Gupta Apr 12 '17 at 12:07
1

I use this snippet myself

if (! ('console' in window) ) {
var names = ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'group', 'groupEnd', 'time', 'timeEnd', 'count', 'trace', 'profile', 'profileEnd'];
window.console = {};
for (var i = 0; i < names.length; ++i) window.console[names[i]] = function() {};
}else {
/*if it exists but doesn't contain all the same methods....silly ie*/
var names = ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'group', 'groupEnd', 'time', 'timeEnd', 'count', 'trace', 'profile', 'profileEnd'];
for (var i = 0; i < names.length; ++i) if(!window.console[names[i]])window.console[names[i]] = function() {};
};
Josh Knutson
  • 325
  • 1
  • 5
  • 11
0

I solved it by using the "fake console" described above, to prevent the script execution breaking. It is included only for InternetExplorer < 10. I include this in my html head:

<!--[if lte IE 10]>
  <script> if (typeof console === "undefined") console = { log: function() { } }; </script>
<![endif]-->
Ruwen
  • 3,008
  • 1
  • 19
  • 16
0
function log(log){
  try{
     console.log( log); 
  }catch(err){}
}
fatnjazzy
  • 6,070
  • 12
  • 57
  • 83
0

I'm using fauxconsole; I modified the css a bit so that it looks nicer but works very well.

Stijn Geukens
  • 15,454
  • 8
  • 66
  • 101