简体   繁体   English

从Google Chrome导出Javascript控制台日志

[英]Export Javascript Console log from Google Chrome

Is there any way to export messages logged to the javascript console in Google Chrome? 有没有办法导出记录到谷歌浏览器的JavaScript控制台的消息?

If not, can anyone suggest a good way to diagnose javascript problems on a client's machine? 如果没有,有人能建议一个很好的方法来诊断客户端机器上的JavaScript问题吗? I have not been able to replicate the problems locally, despite setting up an identical environment. 尽管建立了相同的环境,但我无法在本地复制问题。

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,无论你是在你的开发环境还是你的实时环境中,它都会做正确的事情。

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". 为了简单起见,我们只需对每个日志进行AJAX调用,为了让事情变得更加容易,我们将使用jQuery来完成“繁重的工作”。 (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 seriously consider jQuery!) Oh, and we'll throw in some server-side pseudo-code to demonstrate just how easy that part of the equation is. (如果你使用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)");

There is open-source tool which allows you to save all console.log output in a file on your server - JS LogFlush (plug!). 有一个开源工具,允许您将所有console.log输出保存在服务器上的文件中 - JS LogFlush (插件!)。

JS LogFlush is an integrated JavaScript logging solution which include: JS LogFlush是一个集成的JavaScript日志记录解决方案,包括:

  • cross-browser UI-less replacement of console.log - on client side. 跨浏览器无UI替换console.log - 在客户端。
  • log storage system - on server side. 日志存储系统 - 在服务器端。

Demo 演示

You could use my JavaScript logging library, log4javascript . 您可以使用我的JavaScript日志库log4javascript You will need to: 你需要:

  • Set up log4javascript to send logging messages to the server using AjaxAppender (see the quick start guide , near the bottom) 设置log4javascript以使用AjaxAppender将日志消息发送到服务器(请参阅快速入门指南 ,靠近底部)
  • Add logging calls to your JavaScript 向JavaScript添加日志记录调用
  • Collect logging messages on the server using your technology of choice 使用您选择的技术在服务器上收集日志消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM