Override console.log(); for production
Put this at the top of the file:
var console = {};
console.log = function(){};
For some browsers and minifiers, you may need to apply this onto the window object.
window.console = console;
Override console.log in JavaScript
If you get a Maximum call stack size exceeded error, it almost definitely means your function is recursively calling itself infinitely. The solution you have found, and the one RaraituL has shown, should work perfectly. You are probably calling the part of your code that sets up the call redirection more than once.
// First time:
backupconsolelog = console.log.bind(console);
console.log = function() {
backupconsolelog.apply(this, arguments);
/* Do other stuff */
}
// console.log is now redirected correctly
// Second time:
backupconsolelog = console.log;
// Congratulations, you now have infinite recursion
You could add some debugging information (not using console.log
, obviously, try debugger;
instead to create an automatic breakpoint) where you set up the redirection to see where and when your code is called.
UPDATE
This might belong in a comment: Your console.log
redirection function calls some function called check
, apparently. This check
function then calls console.log
, which if your function - not the original one. Have the check
function call the original implementation instead.
backupconsolelog = console.log.bind(console);
console.log = function() {
check();
backupconsolelog.apply(this, arguments);
}
function check() {
// This will call your function above, so don't do it!
console.log('Foo');
// Instead call the browser's original implementation:
backupconsolelog('Foo');
}
UPDATE 2
The inner workings of the brower's console.log
implementation may or may not depend on the console
being set for the this
reference. Because of this, you should store console.log
bound to console
, like in my code.
Cannot override google app script's console.log()
This is because the log
property of console
is not writable
. You can check that using Object.getOwnPropertyDescriptors
. However, it is still configurable
. So, you can delete
the log
property and set a new value.
let allowLog = false;
(function(){
//test log 1
console.log("self extracting");
//keep ref to global console.log
const _consolelog = console.log;
//test log 2 - verify it works
_consolelog("self extracting2");
//override global console.log
//console.log(Object.getOwnPropertyDescriptors(console));
/*{ toString:
{ value: [Function],
writable: true,
enumerable: true,
configurable: true },
time:
{ value: [Function],
writable: true,
enumerable: true,
configurable: true },
timeEnd:
{ value: [Function],
writable: true,
enumerable: true,
configurable: true },
error:
{ value: [Function],
writable: false,
enumerable: true,
configurable: true },
info:
{ value: [Function],
writable: false,
enumerable: true,
configurable: true },
log:
{ value: [Function],
writable: false,
enumerable: true,
configurable: true },
warn:
{ value: [Function],
writable: false,
enumerable: true,
configurable: true } }*/
delete console.log;
console.log = function (val){
if (allowLog){
_consolelog(val);
_consolelog("allowing");
}else{
_consolelog("log disabled");
}
}
})();
function func1(){
console.log("func1 log msg");
}
Change console.log() for production deployments?
Building off of Yash Rami's answer, I found that I needed to use window.console.log()
in main.ts.
if(environment.production){
window.console.log = function() {}
enableProdMode();
}
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(){}
}
}
Override console.log|error with winston no longer working
Ok I think i found a stable solution with the apply function from winston:
// Override the base console log with winston
console.log = function(){
return logger.info.apply(logger, arguments)
}
console.error = function(){
return logger.error.apply(logger, arguments)
}
console.info = function(){
return logger.warn.apply(logger, arguments)
}
Related Topics
Is There an Internet Explorer Approved Substitute for Selectionstart and Selectionend
The .Replace() Method Does Change the String in Place
Prevent Long Running JavaScript from Locking Up Browser
How to Run Node Js Code from Npm Inside of Swift
iOS Uiwebview JavaScript - Insert Data -Receive Callbacks
Error: Require() of Es Modules Is Not Supported When Importing Node-Fetch
Check If One Date Is Between Two Dates
JavaScript Search Array of Arrays
Youtube Iframe Player API - Onstatechange Not Firing
Calling Member Function of Number Literal
How to Tell If a <Script> Tag Failed to Load
What Is the Lifecycle of an Angularjs Controller
Jquery or JavaScript to Find Memory Usage of Page
Test If a Data Exist in Firebase
How to Use a Numeric Type as an Object Key
Concept - Distilling How a Promise Works