How to Quickly and Conveniently Disable All Console.Log Statements in My Code

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

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.

How do I disable console.log when I am not debugging?

Since 2014, I simply use GULP (and recommend everyone to, it's an amazing tool), and I have a package installed which is called stripDebug which does that for you.

(I also use uglify and closureCompiler in production)


Update (June 20, 2019)

There's a Babel Macro that automatically removes all console statements:

https://www.npmjs.com/package/dev-console.macro

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

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.

Disabling console.log() in production

Solution is to add the polyfill to your polyfill.ts file

if(!window.console) {
var console = {
log : function(){},
warn : function(){},
error : function(){},
time : function(){},
timeEnd : function(){}
}
}

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

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.

How to disable console.log messages based on criteria from specific javascript source (method, file) or message contents

Preamble

The beginning discusses how stuff works in general. If you just care for the code, skip Introduction and scroll to the Solution heading.

Introduction

Problem:

there is a lot of console noise in a web application. A significant amount of that noise is coming from third party code which we do not have access to. Some of the log noise might be coming from our code, as well.

Requirement:

reduce the noise by stopping the log. Some logs should still be kept and the decision about those should be decoupled from the code that is doing the logging. The granularity needed is "per-file". We should be able to choose which files do or do not add log messages. Finally, this will not be used in production code.

Assumption: this will be ran in a developer controlled browser. In that case, I will not focus on backwards compatibility.

Prior work:

First off logging can be enabled/disabled globally using this

(function (original) {    console.enableLogging = function () {        console.log = original;    };    console.disableLogging = function () {        console.log = function () {};    };})(console.log);


Related Topics



Leave a reply



Submit