0

I am looking for a way to capture the entire console dump and store it in javascript variable. I tried to set a global variable in javscript called console_output and then tried to set the valua of the variable console_output = window.console.log,however when I tried to print the console_output variable, it was empty

pilchard
  • 12,414
  • 5
  • 11
  • 23
user670265
  • 483
  • 4
  • 3

3 Answers3

0

Try like this thing

    console.logs = []
    console.logWidthRecord = function(...log)  {
    this.logs.push(...log)
    console.log(...log)
    } 

    console.logWidthRecord('test')
eay
  • 176
  • 6
0

you can use such snippet:

console.stdlog = console.log.bind(console);
console.logs = [];
console.log = function(){
    console.logs.push(Array.from(arguments));
    console.stdlog.apply(console, arguments);
}

We bind console std and recreate log method with logs

Cucunber
  • 161
  • 3
0

You can redefine console.log, while keeping a reference to the original definition. Any call to console.log will then use the new function, in which you can do basically anything with the parameters.

The example below prints the parameters that it receives (by calling the original log function), along with some extra text. Storing the arguments somewhere is just as easily done.

const originalLog = console.log;
console.log = myLog;

function myLog() {
  originalLog(">> myLog begin (length = " + arguments.length + ")");
  originalLog.apply(this, arguments);
  originalLog("<< myLog end");
}

console.log(1, 2, 3);
console.log("Hello World");
console.log();
console.log([true]);
Peter B
  • 22,460
  • 5
  • 32
  • 69