JavaScript Override console.log in JavaScript override javascript console.log,override console js,javascript override console.

I want to override console.log method to invoke a set of tasks whenever console.log is called. I referred other Stackoverflow answers but that give me the error:

Uncaught RangeError: Maximum call stack size exceeded.

This is what I want to do:

backupconsolelog = console.log;

console.log = function(arguments)
{
    //do my tasks;

    backupconsolelog(arguments);
}

Update 1: Somehow managed to over-ride console.log successfully, but I'm now unable to execute console.log(displaySomethingInConsole) in the same .js file where over-riding is done. This somehow causes recursive call to console.log, and gives again Uncaught RangeError: Maximum call stack size exceeded.

How do I use console.log() in the same .js file?

Update 2: I've have a check() function which is called by over-rided console.log. But, there was a console.log call inside the check() function which caused Maximum call stack size exceeded. error.

Update 3: Error again! :(

Uncaught TypeError: Illegal invocation

var _log = window.console.log;
window.console.log = function () {
_log.apply(this, arguments);
check();
};

_log("aaa");
check() {};

Update 4: Binding console to _log, i.e., console.log.bind(console) cleared it.

Answer:1

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.

Answer:2
// keep reference to original function
var _log = window.console.log;

// overwrite function
window.console.log = function () {
    _log.apply(this, arguments);
    alert(arguments);
};

console.log("hello world");
Answer:3

I'm writing a form in Angular and I have one criteria that requires that I need to check for that requires the input to ONLY contain letters and numbers AND the first character cannot be a number. Is ...

I'm writing a form in Angular and I have one criteria that requires that I need to check for that requires the input to ONLY contain letters and numbers AND the first character cannot be a number. Is ...

  1. angular form control validation
  2. angular form control set value
  3. angular form control disabled
  4. angular form control example
  5. angular form control get value
  6. angular form control reset
  7. angular form control name
  8. angular form control checkbox
  9. angular form control default value
  10. angular form control valuechanges
  11. angular form control array
  12. angular form control custom validator
  13. angular form control radio button
  14. angular form control seterrors
  15. angular form control clear value
  16. angular form control readonly
  17. angular form control focus
  18. angular form control change event
  19. angular form control label
  20. angular form control type

I have this Ajax to send multiple images: $('#btn').on("click", function () { var formData = new FormData($("#form1")[0]); var path = "php/upload/adm_prodpictures.php"; $....

I have this Ajax to send multiple images: $('#btn').on("click", function () { var formData = new FormData($("#form1")[0]); var path = "php/upload/adm_prodpictures.php"; $....

  1. javascript variable form data
  2. ajax send formdata and variable

I have a string like this: var variable = 'one|two|three|four|five'; This regex /^(\w+)\|/ matches first word (from left side). In other word, $1 returns one. Now I need to get second word from the ...

I have a string like this: var variable = 'one|two|three|four|five'; This regex /^(\w+)\|/ matches first word (from left side). In other word, $1 returns one. Now I need to get second word from the ...

Say I have an array of strings like this: originalArray = ["some value", "another value", "and another"] How would you add to both the beginning and end of each string so it looks like this: ...

Say I have an array of strings like this: originalArray = ["some value", "another value", "and another"] How would you add to both the beginning and end of each string so it looks like this: ...

  1. append values javascript
  2. append values javascript array