从 Google Chrome 导出 Javascript 控制台日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3462648/
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
Export Javascript Console log from Google Chrome
提问by Dexter
Is there any way to export messages logged to the javascript console in Google Chrome?
有什么方法可以导出记录到 Google Chrome 中 javascript 控制台的消息?
If not, can anyone suggest a good way to diagnose javascript problems on a client's machine? I have not been able to replicate the problems locally, despite setting up an identical environment.
如果没有,任何人都可以提出一种在客户端机器上诊断javascript问题的好方法吗?尽管设置了相同的环境,但我还是无法在本地复制这些问题。
回答by machineghost
Step 1: Add a bunch of console.log statements that will help diagnose your issue
第 1 步:添加一堆有助于诊断问题的 console.log 语句
Step 2: Add logic to re-define console.log on your client's system so that it actually saves its arguments to window.log (or whatever) instead of actually logging them to the console.
第 2 步:添加逻辑以在您的客户端系统上重新定义 console.log,以便它实际将其参数保存到 window.log(或其他),而不是实际将它们记录到控制台。
window.log = []
console = console || {"log":function(x) {window.log.push(x);}}
Step 3: Add an onUnload handler which fires of an AJAX request to your server with the contents of window.log as one of its parameters
第 3 步:添加一个 onUnload 处理程序,该处理程序向您的服务器发出 AJAX 请求,并将 window.log 的内容作为其参数之一
Step 4: Profit! ;-)
第 4 步:获利!;-)
An added bonus to this system is that (once you have it setup) you can use console.log indiscriminately, and whether you're in your dev environment or your live environment it will do the correct thing.
这个系统的一个额外好处是(一旦你设置好了)你可以不加区别地使用 console.log,无论你是在你的开发环境还是你的实时环境中,它都会做正确的事情。
回答by machineghost
Here's a slightly shorter/simpler version of my previous answer. To keep things easy, we'll just make an AJAX call on every log, and to keep things even easier we'll use jQuery to do the "heavy lifting". (If you use a JS library other than jQuery, it should have some sort of similar method; if not, or if you're not using a JS library ... time to seriouslyconsider jQuery!) Oh, and we'll throw in some server-side pseudo-code to demonstrate just how easy that part of the equation is.
这是我之前的答案的一个略短/更简单的版本。为方便起见,我们将只对每个日志进行 AJAX 调用,为了使事情更简单,我们将使用 jQuery 来完成“繁重的工作”。(如果你使用 jQuery 以外的 JS 库,它应该有某种类似的方法;如果没有,或者如果你没有使用 JS 库......是时候认真考虑 jQuery!)哦,我们会抛出在一些服务器端伪代码中,以演示等式的那部分是多么容易。
Step 1: Add a bunch of console.log statements that will help diagnose your issue
第 1 步:添加一堆有助于诊断问题的 console.log 语句
Step 2: Add logic to re-define console.log on your client's system so that it sends the log to your server if there is no console
第 2 步:添加逻辑以在您的客户端系统上重新定义 console.log,以便在没有控制台时将日志发送到您的服务器
var SERVER_URL = "www.yourServer.com/ajaxLogger.jsp";
var ajaxConsole = {"log":function(x) {
$.get(SERVER_URL, {"log":x});
}}
console = console || ajaxConsole; // If there is no console, use the AJAX one
Step 3: Create a logging page on your server
第 3 步:在您的服务器上创建一个日志页面
The following example uses pseudo-code, since everyone has a different server-side language:
以下示例使用伪代码,因为每个人都有不同的服务器端语言:
String log = pageParameters["log"];
Database.execute("INSERT INTO yourLogTable (log, date) VALUES ('" +
dbEscape(log) +"', current date)");
回答by hindmost
There is open-source tool which allows you to save all console.logoutput in a file on your server - JS LogFlush(plug!).
有一个开源工具可以让您将所有console.log输出保存在服务器上的文件中 - JS LogFlush(插件!)。
JS LogFlushis an integrated JavaScript logging solution which include:
- cross-browser UI-less replacement of console.log - on client side.
- log storage system - on server side.
JS LogFlush是一个集成的 JavaScript 日志解决方案,其中包括:
- 跨浏览器 UI-less 替换 console.log - 在客户端。
- 日志存储系统 - 在服务器端。
回答by Tim Down
You could use my JavaScript logging library, log4javascript. You will need to:
您可以使用我的 JavaScript 日志库log4javascript。您将需要:
- Set up log4javascript to send logging messages to the server using AjaxAppender (see the quick start guide, near the bottom)
- Add logging calls to your JavaScript
- Collect logging messages on the server using your technology of choice
- 设置 log4javascript 以使用 AjaxAppender 将日志消息发送到服务器(请参阅快速入门指南,靠近底部)
- 向 JavaScript 添加日志调用
- 使用您选择的技术在服务器上收集日志消息

