# Safely using console.* in browser

I come from a Java background so my desire for proper unambiguous logging is strong. I prefer using the console to using some other gui widget however, in the browser I know that I can't always count on the console to exist or for it to have all the levels I might like. To remedy this I worked up the following console sanitizer and I was curious what drawbacks it might have.

function configureConsoleLog() {
"use strict";
var logMethods = [ 'trace', 'debug', 'log', 'info', 'warn', 'error' ], i;

if (!window.console) {
window.console = {log: function (args) {}}; //noop
}

for (i = 0; i < logMethods.length; i += 1) {
if (!window.console[logMethods[i]]) {
window.console[logMethods[i]] = window.console.log;
}
}
}


The idea is to find out if console exists and if it does try to find out which of the supported levels I need are available. The first level of fallback is to map unsupported methods to console.log. The second level is to create my own console variable and make a noop log function since I have nowhere to send the output. This approach leaves open the possibility of coming up with a different strategy if console doesn't exist but for now I'm content to ignore logging if there is no console.

As a side note I looked at several JS logging packages but all of them have the fatal drawback of obscuring original line numbers, usually by delegating to a console method which only reports the line number of the delegating statement.

Thoughts?

• I think I saw a really good discussion of this exact problem. Perhaps on StackExchange - did you check there? – Donald.McLean Apr 25 '12 at 4:32
• I have searched a bit but couldn't find anything that matched my problem. However, I will take another look on SO. – adambender Apr 25 '12 at 4:33
• The post I saw was this one: stackoverflow.com/questions/690251/… (I meant StackOverflow in my comment) – Donald.McLean Apr 25 '12 at 15:46
• Looks like the advice is consistent with my approach, thanks for the pointer. – adambender Apr 25 '12 at 16:56

I don't see any problems with that. It's similar to this answer on SO.

Just a couple minor things:

• You don't need args in the empty log function

• You should make the entire function anonymous and execute it immediately (as it's being done in the answer I linked to); I don't think there's any usecase for calling it otherwise

• Don't forget var in front of the i variable, otherwise it becomes global

• First, thanks for the tips! I think args was actually vestigial and definitely should be removed. This particular function is being called from inside of another framework as a part of a config/bootstrap process so it works best as a function in this case. Lastly, the i variable is declared at the top of the function (jslint enforced), it's just hanging on the end of the array initialization, probably should be moved to the front so it doesn't get lost. – adambender Apr 25 '12 at 16:52
• @adambender: ah yeah, I missed the i at the end :) – seand Apr 25 '12 at 16:55