Is there any way to turn off all console.log
statements in my JavaScript code, for testing purposes?
I have used winston logger earlier.
Nowadays I am using below simpler code from experience:
Set the environment variable from cmd/ command line (on Windows):
cmd
setx LOG_LEVEL info
Or, you could have a variable in your code if you like, but above is better.
Restart cmd/ command line, or, IDE/ editor like Netbeans
Have below like code:
console.debug = console.log; // define debug function
console.silly = console.log; // define silly function
switch (process.env.LOG_LEVEL) {
case 'debug':
case 'silly':
// print everything
break;
case 'dir':
case 'log':
console.debug = function () {};
console.silly = function () {};
break;
case 'info':
console.debug = function () {};
console.silly = function () {};
console.dir = function () {};
console.log = function () {};
break;
case 'trace': // similar to error, both may print stack trace/ frames
case 'warn': // since warn() function is an alias for error()
case 'error':
console.debug = function () {};
console.silly = function () {};
console.dir = function () {};
console.log = function () {};
console.info = function () {};
break;
}
Now use all console.* as below:
console.error(' this is a error message '); // will print
console.warn(' this is a warn message '); // will print
console.trace(' this is a trace message '); // will print
console.info(' this is a info message '); // will print, LOG_LEVEL is set to this
console.log(' this is a log message '); // will NOT print
console.dir(' this is a dir message '); // will NOT print
console.silly(' this is a silly message '); // will NOT print
console.debug(' this is a debug message '); // will NOT print
Now, based on your LOG_LEVEL settings made in the point 1 (like, setx LOG_LEVEL log
and restart command line), some of the above will print, others won't print
Hope that helped.
I know you asked how to disable console.log, but this might be what you're really after. This way you don't have to explicitly enable or disable the console. It simply prevents those pesky console errors for people who don't have it open or installed.
if(typeof(console) === 'undefined') {
var console = {};
console.log = console.error = console.info = console.debug = console.warn = console.trace = console.dir = console.dirxml = console.group = console.groupEnd = console.time = console.timeEnd = console.assert = console.profile = function() {};
}
Just change the flag DEBUG
to override the console.log function. This should do the trick.
var DEBUG = false;
// ENABLE/DISABLE Console Logs
if(!DEBUG){
console.log = function() {}
}
This a hybrid of answers from SolutionYogi and Chris S. It maintains the console.log line numbers and file name. Example jsFiddle.
// Avoid global functions via a self calling anonymous one (uses jQuery)
(function(MYAPP, $, undefined) {
// Prevent errors in browsers without console.log
if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function(){};
//Private var
var console_log = console.log;
//Public methods
MYAPP.enableLog = function enableLogger() { console.log = console_log; };
MYAPP.disableLog = function disableLogger() { console.log = function() {}; };
}(window.MYAPP = window.MYAPP || {}, jQuery));
// Example Usage:
$(function() {
MYAPP.disableLog();
console.log('this should not show');
MYAPP.enableLog();
console.log('This will show');
});
This should override all methods of window.console. You can put it on the very top of your scripts section, and if you are on a PHP framework you can only print this code when your app environment is production, or if some kind of debug flag is disabled. Then you would have all your logs in your code working on development environments or in debug mode.
window.console = (function(originalConsole){
var api = {};
var props = Object.keys(originalConsole);
for (var i=0; i<props.length; i++) {
api[props[i]] = function(){};
}
return api;
})(window.console);
Warning: Shameless plug!
You could also use something like my JsTrace object to have modular tracing with module-level "switching" capability to only turn on what you want to see at the time.
http://jstrace.codeplex.com
(Also has a NuGet package, for those who care)
All levels default to "error", though you can shut them "off". Though, I can't think of why you would NOT want to see errors
You can change them like this:
Trace.traceLevel('ModuleName1', Trace.Levels.log);
Trace.traceLevel('ModuleName2', Trace.Levels.info);
Fore more docs, check out the Documentation
T