82

I have a bunch of console.log() calls in my JavaScript.

Should I comment them out before I deploy to production?

I'd like to just leave them there, so I don't have to go to the trouble of re-adding the comments later on if I need to do any more debugging. Is this a bad idea?

DavidRR
  • 18,291
  • 25
  • 109
  • 191
Charlie Kotter
  • 1,849
  • 3
  • 16
  • 13
  • 1
    For those looking for the Angular version of this question: https://stackoverflow.com/questions/42307317/stripping-all-comments-and-console-logs-with-ng-build-prod-possible – rmcsharry May 10 '18 at 21:35
  • Related [deactivate console log on production](https://medium.com/@hfally/deactivate-console-log-on-production-why-and-how-d0345abbe71) how and why – surfmuggle Mar 30 '23 at 09:54

12 Answers12

121

It will cause Javascript errors, terminating the execution of the block of Javascript containing the error.

You could, however, define a dummy function that's a no-op when Firebug is not active:

if(typeof console === "undefined") {
    console = { log: function() { } };
}

If you use any methods other than log, you would need to stub out those as well.

Jason C
  • 21,377
  • 10
  • 38
  • 33
  • Thanks. That looks like a nice workaround. Let me see if I understand it. You're saying if the console is undefined, then set the console to an empty function. I don't understand the colon syntax after 'log'. What does that do and why is everything inside braces after "console=" ? – Charlie Kotter Jul 11 '09 at 17:30
  • 2
    The braces define an object literal: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Literals#Object_Literals and the "function() {}" bit defines an anonymous function that takes no arguments and does nothing. (It still works to call "console.log(foo)" with an argument because JS doesn't care if you call a function with too many or too few arguments.) – Jason C Jul 11 '09 at 17:37
  • 1
    Excellent solution man!! :-) I just had this problem the other day. My page was working working in FF only when FireBug was on and it would randomly halt on certain actions in IE. I was like WTF!?! And, then I realized I had the console.log calls still in there and I had to go through and remove them. I will likely add your solution to my main JS file now and never have to worry about that unfortunate scenario again. – KyleFarris Jul 11 '09 at 19:42
  • 6
    I've got a little (unit tested) project that helps you do exactly this with various different console methods in the least amount of code possible. It's located here: https://github.com/andyet/ConsoleDummy.js – Henrik Joreteg Jan 28 '11 at 23:17
  • It might be trivial to others, but in case someone's wondering when `console` would become `undefined`, the answer is, when you do it "yourself". – aderchox Oct 16 '22 at 11:51
37

As others have already pointed it, leaving it in will cause errors in some browsers, but those errors can be worked around by putting in some stubs.

However, I would not only comment them out, but outright remove those lines. It just seems sloppy to do otherwise. Perhaps I'm being pedantic, but I don't think that "production" code should include "debug" code at all, even in commented form. If you leave comments in at all, those comments should describe what the code is doing, or the reasoning behind it--not blocks of disabled code. (Although, most comments should be removed automatically by your minification process. You are minimizing, right?)

Also, in several years of working with JavaScript, I can't recall ever coming back to a function and saying "Gee, I wish I'd left those console.logs in place here!" In general, when I am "done" with working on a function, and later have to come back to it, I'm coming back to fix some other problem. Whatever that new problem is, if the console.logs from a previous round of work could have been helpful, then I'd have spotted the problem the first time. In other words, if I come back to something, I'm not likely to need exactly the same debug information as I needed on previous occasions.

Just my two cents... Good luck!


Update after 13 years

I've changed my mind, and now agree with the comments that have accumulated on this answer over the years.

Some log messages provide long-term value to an application, even a client-side JavaScript application, and should be left in.

Other log messages are low-value noise and should be removed, or else they will drown out the high-value messages.

Chris Nielsen
  • 14,731
  • 7
  • 48
  • 54
  • 1
    the problem is often during the production cycle as something is being debugged or tested, people view it outside of the development team who do not have a dev console. my personal preference is not to have the app die on them so we don't get calls like 'your new code does nothing for me'. – Dimitar Christoff Jun 30 '10 at 20:06
  • 10
    Permanent debug lines are fantastic. They mean that you often don't have to look around for the source of a bug, but can see right away what is happening. There are many sections of code where a log statement won't make any significant performance difference. – Casebash Jan 11 '13 at 06:43
  • 4
    Good logging should be useful for a variety of purposes. Saying that log() statements were only useful for debugging one issue, reminds me of Java logging by people who don't know how to write proper logging. – Thomas W May 02 '13 at 03:48
  • 7
    In short: Log major (high-level) decisions & event handling, so the business, high-level UI & major event-handling flow can be followed. These are valuable in perpetuity for engineering the application. Logging 'entering method A', 'leaving method A', 'param1 77' is OTOH not good logging. – Thomas W May 02 '13 at 03:50
10

If you have a deployment script, you can use it to strip out the calls to console.log (and minify the file).

While you're at it, you can throw your JS through JSLint and log the violations for inspection (or prevent the deployment).

This is a great example of why you want to automate your deployment. If your process allows you to publish a js file with console.logs in it, at some point you will do it.

Nosredna
  • 83,000
  • 15
  • 95
  • 122
10

To my knowledge there is no shorter method of stubbing out console.log than the following 45 characters:

window.console||(console={log:function(){}});

That's the first of 3 different versions depending on which console methods you want to stub out all of them are tiny and all have been tested in IE6+ and modern browsers.

The other two versions cover varying other console methods. One covers the four basics and the other covers all known console methods for firebug and webkit. Again, in the tiniest file sizes possible.

That project is on github: https://github.com/andyet/ConsoleDummy.js

If you can think of any way to minimize the code further, contributions are welcomed.

-- EDIT -- May 16, 2012

I've since improved on this code. It's still tiny but adds the ability to turn the console output on and off: https://github.com/HenrikJoreteg/andlog

It was featured on The Changelog Show

Henrik Joreteg
  • 1,698
  • 16
  • 18
5

You should at least create a dummy console.log if the object doesn't exist so your code won't throw errors on users' machines without firebug installed.

Another possibility would be to trigger logging only in 'debug mode', ie if a certain flag is set:

if(_debug) console.log('foo');
_debug && console.log('foo');
Christoph
  • 164,997
  • 36
  • 182
  • 240
  • Agree with Cristoph, for Safari it won't be a problem but in other browsers it will throw errors and might stop javascript to continue. In general not a good idea... – Sinan Jul 11 '09 at 17:22
5

Hope it helps someone--I wrote a wrapper for it a while back, its slightly more flexible than the accepted solution.

Obviously, if you use other methods such as console.info etc, you can replicate the effect. when done with your staging environment, simply change the default C.debug to false for production and you won't have to change any other code / take lines out etc. Very easy to come back to and debug later on.

var C = {
    // console wrapper
    debug: true, // global debug on|off
    quietDismiss: false, // may want to just drop, or alert instead
    log: function() {
        if (!C.debug) return false;

        if (typeof console == 'object' && typeof console.log != "undefined") {
            console.log.apply(this, arguments); 
        }
        else {
            if (!C.quietDismiss) {
                var result = "";
                for (var i = 0, l = arguments.length; i < l; i++)
                    result += arguments[i] + " ("+typeof arguments[i]+") ";

                alert(result);
            }
        }
    }
}; // end console wrapper.

// example data and object
var foo = "foo", bar = document.getElementById("divImage");
C.log(foo, bar);

// to surpress alerts on IE w/o a console:
C.quietDismiss = true;
C.log("this won't show if no console");

// to disable console completely everywhere:
C.debug = false;
C.log("this won't show ever");
Dimitar Christoff
  • 26,147
  • 8
  • 50
  • 69
4

this seems to work for me...

if (!window.console) {
    window.console = {
        log: function () {},
        group: function () {},
        error: function () {},
        warn: function () {},
        groupEnd: function () {}
    };
}
nick fox
  • 570
  • 8
  • 15
3

Figured I would share a different perspective. Leaving this type of output visible to the outside world in a PCI application makes you non-compliant.

McGovernTheory
  • 6,556
  • 4
  • 41
  • 75
2

I agree that the console stub is a good approach. I've tried various console plugins, code snippets, including some fairly complex ones. They all had some problem in at least one browser, so I ended up going with something simple like below, which is an amalgamation of other snippets I've seen and some suggestions from the YUI team. It appears to function in IE8+, Firefox, Chrome and Safari (for Windows).

// To disable logging when posting a production release, just change this to false.
var debugMode = false;

// Support logging to console in all browsers even if the console is disabled. 
var log = function (msg) {
    debugMode && window.console && console.log ? console.log(msg) : null;
};

Note: It supports disabling logging to the console via a flag. Perhaps you could automate this via build scripts too. Alternatively, you could expose UI or some other mechanism to flip this flag at run time. You can get much more sophisticated of course, with logging levels, ajax submission of logs based on log threshold (e.g. all Error level statements are transmitted to the server for storage there etc.).

Many of these threads/questions around logging seem to think of log statements as debug code and not code instrumentation. Hence the desire to remove the log statements. Instrumentation is extremely useful when an application is in the wild and it's no longer as easy to attach a debugger or information is fed to you from a user or via support. You should never log anything sensitive, regardless of where it's been logged to so privacy/security should not be compromised. Once you think of the logging as instrumentation it now becomes production code and should be written to the same standard.

With applications using ever more complex javascript I think instrumentation is critical.

tidmutt
  • 143
  • 1
  • 9
1

As other have mentions it will thrown an error in most browsers. In Firefox 4 it won't throw an error, the message is logged in the web developer console (new in Firefox 4).

One workaround to such mistakes that I really liked was de&&bug:

var de = true;
var bug = function() { console.log.apply(this, arguments); }

// within code
de&&bug(someObject);
mhitza
  • 5,709
  • 2
  • 29
  • 52
0

A nice one-liner:

(!console) ? console.log=function(){} : console.log('Logging is supported.');
Pedro
  • 17
  • 1
0

Yes, it's bad idea to let them running always in production. What you can do is you can use console.debug which will console ONLY when the debugger is opened.

https://developer.mozilla.org/en-US/docs/Web/API/console/debug

V. Sambor
  • 12,361
  • 6
  • 46
  • 65