如何在 JavaScript 中从 Chrome 的控制台读取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19846078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read from Chrome's console in JavaScript
提问by Mike
I would like to put a button in my app where if you press it, it will get the contents of everything that was written to the console and email it to me (for reporting bugs). I know I can keep a variable around and every time I do a console.log also append the message to that variable but I am trying to keep the memory consumption of the app low so it would be much more efficient just to grab it from the console.
我想在我的应用程序中放置一个按钮,如果您按下它,它将获取写入控制台的所有内容并将其通过电子邮件发送给我(用于报告错误)。我知道我可以保留一个变量,每次执行 console.log 时也会将消息附加到该变量,但我试图将应用程序的内存消耗保持在较低水平,因此仅从安慰。
Is there a way to retrieve the console messages from javascript?
有没有办法从javascript检索控制台消息?
Thanks
谢谢
回答by Denys Séguret
You can't. What's in the console can't be read from JavaScript.
你不能。无法从 JavaScript 读取控制台中的内容。
What you can do is hook the console.log
function so that you store when it logs :
您可以做的是挂钩该console.log
功能,以便在记录时进行存储:
console.stdlog = console.log.bind(console);
console.logs = [];
console.log = function(){
console.logs.push(Array.from(arguments));
console.stdlog.apply(console, arguments);
}
console.logs
contains all what was logged. You can clean it at any time by doing console.logs.length = 0;
.
console.logs
包含所有记录的内容。您可以随时通过执行 来清洁它console.logs.length = 0;
。
You can still do a standard, non storing, log by calling console.stdlog
.
您仍然可以通过调用console.stdlog
.
回答by xgqfrms-gildata
get all console data
获取所有控制台数据
how to read browser console error in js?
如何在js中读取浏览器控制台错误?
How to read from Chrome's console in JavaScript
如何在 JavaScript 中从 Chrome 的控制台读取
https://www.quora.com/How-do-I-read-console-window-errors-from-Chrome-using-JavaScript
https://www.quora.com/How-do-I-read-console-window-errors-from-Chrome-using-JavaScript
logs
日志
console.defaultLog = console.log.bind(console);
console.logs = [];
console.log = function(){
// default & console.log()
console.defaultLog.apply(console, arguments);
// new & array data
console.logs.push(Array.from(arguments));
}
error
错误
console.defaultError = console.error.bind(console);
console.errors = [];
console.error = function(){
// default & console.error()
console.defaultError.apply(console, arguments);
// new & array data
console.errors.push(Array.from(arguments));
}
warn
警告
console.defaultWarn = console.warn.bind(console);
console.warns = [];
console.warn = function(){
// default & console.warn()
console.defaultWarn.apply(console, arguments);
// new & array data
console.warns.push(Array.from(arguments));
}
debug
调试
console.defaultDebug = console.debug.bind(console);
console.debugs = [];
console.debug = function(){
// default & console.debug()
console.defaultDebug.apply(console, arguments);
// new & array data
console.debugs.push(Array.from(arguments));
}
回答by QA Collective
I have used this code in the past to capture all console activityand store it with typesand timestampsin console.everything
for sending back to the server for diagnosing form data entry issues. I run this code as early as possible in the <head>
element.
我过去曾使用此代码来捕获所有控制台活动并将其与类型和时间戳一起存储,console.everything
以便发送回服务器以诊断表单数据输入问题。我尽可能早地在<head>
元素中运行此代码。
if (console.everything === undefined)
{
console.everything = [];
console.defaultLog = console.log.bind(console);
console.log = function(){
console.everything.push({"type":"log", "datetime":Date().toLocaleString(), "value":Array.from(arguments)});
console.defaultLog.apply(console, arguments);
}
console.defaultError = console.error.bind(console);
console.error = function(){
console.everything.push({"type":"error", "datetime":Date().toLocaleString(), "value":Array.from(arguments)});
console.defaultError.apply(console, arguments);
}
console.defaultWarn = console.warn.bind(console);
console.warn = function(){
console.everything.push({"type":"warn", "datetime":Date().toLocaleString(), "value":Array.from(arguments)});
console.defaultWarn.apply(console, arguments);
}
console.defaultDebug = console.debug.bind(console);
console.debug = function(){
console.everything.push({"type":"debug", "datetime":Date().toLocaleString(), "value":Array.from(arguments)});
console.defaultDebug.apply(console, arguments);
}
}