0

I have a web application on javascript/php and I'm making a bug finder function, a button on the front-end where the user can inform a bug. The current function:

function bug_finder(){

    var txt = document.getElementById('bug_txt').value;

    if(txt==''){
        alert_fail('Um campo ficou vazio!');
        return;
    }

    var DATA = { acao:'bug_finder', txt:txt, log:console.log };

    console.log( JSON.stringify(DATA) );

    $.ajax({
        type: "POST",
        url: 'dash_acao.php',
        timeout:2000,
        data: DATA,
        success: function(response){

            console.log(response);
            alert_sucesso('');

        },
        error: function(response){
            alert_fail('');
        }
    });

}

The ajax sends the console.log content of the browser to a php file where I do database updates and create the log file in the server so I can look it up.

Until now I have not found a way to get the console.log from the browser into data, so I can manipulate.

1 Answers1

0

You can overwrite console.log with a custom function. See snippet below. Note that there's no limit regarding the parameter count for console.log, so you will end up having arrays of arrays.

// remember original console.log
const origLog = console.log;
// here, the contents of console.log will be stored
let consoleBuffer = [];

// replace console.log with own function
console.log = function (...args) {
   // I would not store an infinite amount of entries,
   // so remove oldest one if 10 stored
   if (consoleBuffer.length === 10) consoleBuffer.pop();
   
   // remember
   consoleBuffer.push(args);
   
   // call original function
   origLog.apply(console, args);
};

console.log('foo');
let err = 'unknown error';
console.log('An error occured: ', err);
// use warn since this was not replaced
console.warn(consoleBuffer);

EDIT (ES5 code)

'use strict';

// remember original console.log
var origLog = console.log;
// here, the contents of console.log will be stored
var consoleBuffer = [];

// replace console.log with own function
console.log = function () {
   var args = Array.prototype.slice.call(arguments);
   
   // I would not store an infinite amount of entries,
   // so remove oldest one if 10 stored
   if (consoleBuffer.length === 10) consoleBuffer.pop();

   // remember
   consoleBuffer.push(args);

   // call original function
   origLog.apply(console, args);
};

console.log('foo');
var err = 'unknown error';
console.log('An error occured: ', err);
// use warn since this was not replaced
console.warn(consoleBuffer);
SVSchmidt
  • 6,269
  • 2
  • 26
  • 37
  • Man oh man, I think that is it. I'm gonna test it right now, but I think it's gonna work. Thanks. – Lucas Martins Jul 16 '17 at 19:31
  • That erros show up `Uncaught TypeError: CreateListFromArrayLike called on non-object at Object.console.log ((index):3532) at (index):3535` – Lucas Martins Jul 17 '17 at 23:05
  • @LucasMartins does the ES5 code (see Edit) work for you? – SVSchmidt Jul 18 '17 at 07:19
  • the problem was in that line: origLog.apply(console, args); I changed to console.warn(args); and it worked. – Lucas Martins Jul 18 '17 at 18:34
  • @LucasMartins Glad I could help. Please consider marking my answer as "accepted" when it was useful to you. However, I still don't get the error. At least the ES5 code should work. – SVSchmidt Jul 19 '17 at 06:52