1

Is there a cross browser way of implementing console.log functionality, the official supported browser at one of my client is still IE7/IE8.

As development is tested with Firefox for its debugging capabilities, often come across issues on release as some obscure flow still left with un commented console.log statements which doesn't fly with IE

looking for kind of, if anybody does use an elegant way, would love to learn.

function log(msg){
   if(IE) alert(msg)
   else
     console.log(msg)
}
Math chiller
  • 3,931
  • 5
  • 23
  • 40
Venkat
  • 1,181
  • 1
  • 12
  • 23
  • 5
    Just make a fake `console` object if there isn't one already – SLaks Aug 28 '13 at 20:25
  • 1
    Production code generally shouldn't need to output to the console – zzzzBov Aug 28 '13 at 20:26
  • 1
    Does this help? IE8 [Internet Explorer Console](http://stackoverflow.com/questions/2656730/internet-explorer-console) – dc5 Aug 28 '13 at 20:27
  • 1
    An alternate solution is to run your scripts through a [linter](http://jshint.com/) which will alert you to any `console.log` calls which remain so that you can remove them. – Mathletics Aug 28 '13 at 20:28
  • 1
    Obligatory: [What is an XY problem?](http://meta.stackexchange.com/questions/66377) – Mathletics Aug 28 '13 at 20:29
  • @dc5 the question is how to prevent existing `log` calls from bombing the page in IE, not how to view the console in IE. – Mathletics Aug 28 '13 at 20:30
  • @Mathletics - partially correct. The first part of the question was "Is there a cross browser way of implementing console.log functionality," and IE 8 was mentioned as not supporting it. Thought that might be information the OP didn't already have. – dc5 Aug 28 '13 at 20:34
  • why in the world would you want cross-browser console.log?????????????? – Math chiller Aug 28 '13 at 20:37
  • Most modern browsers (including IE) has console.log so cross browser version may not be even needed. Drop support for IE8 and you're sorted :). – demee Aug 28 '13 at 21:09

2 Answers2

2

Here is an option:

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

EDIT: as noted by others, you really should make sure that console.log doesn't end up in production code.

zigdawgydawg
  • 1,861
  • 10
  • 9
  • 3
    I don't necessarily think that console.log is super bad thing on production code. As long as it doesn't break anything some logs like "application loaded", "controler initialized" etc may not be a big deal. unless it breaks stuff, then yeah, it may be a problem. – demee Aug 28 '13 at 21:08
  • @zigdawgydawg This looks promising, i will test it out and accept. – Venkat Aug 28 '13 at 21:26
2

Take a look at something like:

log4javascript

or

log4js

They maybe what you are looking for (there are others too)

Then you can detect if the console object is available on the browser you are using and if not then assign the appropriate functions from those libraries.

Though reading your question, it does seem like an XY problem.

Xotic750
  • 20,394
  • 8
  • 50
  • 71