2017-07-16 93 views
0

我在javascript/php上有一个web应用程序,我正在制作一个bug查找程序功能,前端的一个用户可以通知bug的按钮。 目前的功能:将console.log的浏览器导入数据

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(''); 
     } 
    }); 

} 

阿贾克斯将浏览器的PHP文件,我做数据库更新,并在服务器中创建日志文件,所以我可以看看它的内容的console.log。

直到现在我还没有找到一种方法来从浏览器中获取console.log数据,所以我可以操纵。

+0

*“...所以我可以操纵”*。你不能操纵什么? –

+0

可能的重复https://stackoverflow.com/questions/19846078/how-to-read-from-chromes-console-in-javascript – Kdawg

回答

0

您可以用自定义函数覆盖console.log。请参阅下面的代码片段。请注意,关于console.log的参数计数没有限制,所以您最终将拥有阵列数组。

// 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代码)

'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);

+0

男人哦,我认为就是这样。我现在要测试它,但我认为它会工作。谢谢。 –

+0

这误差修改显示 '遗漏的类型错误:CreateListFromArrayLike在Object.console.log((指数):3532)呼吁非对象 在(指数):3535' –

+0

@LucasMartins请问ES5代码(请参阅编辑)为你工作? – SVSchmidt

0

你不能让你在执行console.log打印的数据()。 我不明白你为什么需要它。而不是打印它使用console.log()你可以“操纵”的回应? 或者如果你想要它到另一个PHP/HTML文件,只需发送它。

+0

该函数是一个bug_finder,我需要从浏览器中获取信息以知道孔系统上发生了什么。这是一个在两个不同城市使用的大系统。有时会出现错误,我需要一个报告工具,以便用户可以描述发生的情况,因此在阅读我想要发送的浏览器日志时,它给了我一个小小的指导。 –