Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quickly and conveniently disable all console.log statements in my code?

Is there any way to turn off all console.log statements in my JavaScript code, for testing purposes?

like image 488
Zack Burt Avatar asked Jul 31 '09 23:07

Zack Burt


People also ask

How can we prevent console log () statements from getting into our code?

Just change the flag DEBUG to override the console. log function. This should do the trick. If using Angular, you can use it as a global configuration in your application.

Does console log slow down code?

Yes, it will reduce the speed, though only negligibly. But, don't use it as it's too easy for a person to read your logs.


Video Answer


2 Answers

Redefine the console.log function in your script.

console.log = function() {} 

That's it, no more messages to console.

EDIT:

Expanding on Cide's idea. A custom logger which you can use to toggle logging on/off from your code.

From my Firefox console:

var logger = function() {     var oldConsoleLog = null;     var pub = {};      pub.enableLogger =  function enableLogger()                          {                             if(oldConsoleLog == null)                                 return;                              window['console']['log'] = oldConsoleLog;                         };      pub.disableLogger = function disableLogger()                         {                             oldConsoleLog = console.log;                             window['console']['log'] = function() {};                         };      return pub; }();  $(document).ready(     function()     {         console.log('hello');          logger.disableLogger();         console.log('hi', 'hiya');         console.log('this wont show up in console');          logger.enableLogger();         console.log('This will show up!');     }  ); 

How to use the above 'logger'? In your ready event, call logger.disableLogger so that console messages are not logged. Add calls to logger.enableLogger and logger.disableLogger inside the method for which you want to log messages to the console.

like image 153
SolutionYogi Avatar answered Nov 02 '22 23:11

SolutionYogi


The following is more thorough:

var DEBUG = false; if(!DEBUG){     if(!window.console) window.console = {};     var methods = ["log", "debug", "warn", "info"];     for(var i=0;i<methods.length;i++){         console[methods[i]] = function(){};     } } 

This will zero out the common methods in the console if it exists, and they can be called without error and virtually no performance overhead. In the case of a browser like IE6 with no console, the dummy methods will be created to prevent errors. Of course there are many more functions in Firebug, like trace, profile, time, etc. They can be added to the list if you use them in your code.

You can also check if the debugger has those special methods or not (ie, IE) and zero out the ones it does not support:

if(window.console && !console.dir){ var methods = ["dir", "dirxml", "trace", "profile"]; //etc etc     for(var i=0;i<methods.length;i++){         console[methods[i]] = function(){};     } } 
like image 21
mwilcox Avatar answered Nov 03 '22 00:11

mwilcox