使用 Firefox 将 JavaScript 控制台记录到日志文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19298420/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:03:48  来源:igfitidea点击:

Log JavaScript console into a log file with Firefox

javascriptfirefoxlogging

提问by NagyI

We have a web application which runs in a kiosk mode Firefox, using the RKiosk extension to achieve this. We suspect that we have a very rare error in the system which yields in a JavaScript error. However because we can't access the JavaScript console we can't examine the log.

我们有一个在 kiosk 模式下运行的 web 应用程序 Firefox,使用 RKiosk 扩展来实现这一点。我们怀疑系统中有一个非常罕见的错误,导致 JavaScript 错误。但是,由于我们无法访问 JavaScript 控制台,因此无法检查日志。

I'm searching for an option to make Firefox log all JavaScript console messages into a file regardless of the tab and page opened. I can't seem to find any extension for this. I'm already using log4javascript which sends errors back to the server, but it seems that our application crashes in a way that it skips the logging altogether.

我正在寻找一个选项,让 Firefox 将所有 JavaScript 控制台消息记录到一个文件中,而不管打开的选项卡和页面如何。我似乎找不到任何扩展名。我已经在使用 log4javascript,它将错误发送回服务器,但似乎我们的应用程序崩溃了,它完全跳过了日志记录。

采纳答案by vbail

Have you tried jserrorcollector? We are using it and it works fine (only in Firefox). It's only for Java.

你试过 jserrorcollector 吗?我们正在使用它并且它运行良好(仅在 Firefox 中)。它仅适用于 Java。

// Initialize
FirefoxProfile ffProfile = null;
ffProfile = new FirefoxProfile();
JavaScriptError.addExtension(ffProfile);

 // Get the errors
List<JavaScriptError> jsErrors = JavaScriptError.readErrors(webDriver);

More information: https://github.com/mguillem/JSErrorCollector

更多信息:https: //github.com/mguillem/JSErrorCollector

回答by Benjamin Gruenbaum

Writing to a file sounds like a tedious task to me. It requires privileges that browser code doesn't normally have and you'd have to negotiate with an add-on you'd have to write in order to access file I/O.

写入文件对我来说听起来像是一项乏味的任务。它需要浏览器代码通常没有的权限,并且您必须与您必须编写的附加组件协商才能访问文件 I/O

From what I understand your issue is

据我了解你的问题是

I'd like to make Firefox log all errors

我想让 Firefox 记录所有错误

There are several approaches we can do to tackle this

我们可以采取多种方法来解决这个问题

First approach - log everything to localStorage too:

第一种方法 - 也将所有内容记录到 localStorage:

Now, rather than writing to an actual file, you can write to localStorageor IndexedDBinstead.

现在,您可以写入localStorageIndexedDB代替写入实际文件,而不是写入实际文件。

localStorage["myApplog"] = localStorage["myApplog"] || "";
var oldLog = console.log;
console.log = function(){
    oldLog.apply(console,arguments); // use the old console log
    var message =  "\n "+(new Date).toISOString() + " :: "+
                   Array.prototype.join.call(arguments," , "); // the arguments
    localStorage["myApplog"] += message; 
}

This is rather dirty and rather slow, but it should get the job done and you can access the log later in local storage. LocalStorage has a ~5MB limit if I recall correctly which I think is enough if you don't go crazy with logging. You can also run it selectively.

这相当脏而且相当慢,但它应该可以完成工作,您可以稍后在本地存储中访问日志。如果我没记错的话,LocalStorage 有大约 5MB 的限制,如果您不为日志记录发疯,我认为这已经足够了。您也可以有选择地运行它。

Second approach - log only errors

第二种方法 - 只记录错误

This is similar to what Pumbaa80 suggested. You can simply override window.onerrorand only log errors.

这类似于 Pumbaa80 的建议。您可以简单地覆盖window.onerror并仅记录错误。

// put an empty string in loggedWinErrors first
var oldError = window.onerror || function(){};
window.onerror = function(err,url,lineNumber){
   oldError.call(this,err,url,lineNumber);
   var err ="\n Error: (file: " + url+", error: "+err+", lineNumber: "+lineNumber+")"); 
   localStorage["loggedWinErrors"] += err;
}

Third and drastic approach - use a VM.

第三个也是激进的方法 - 使用虚拟机。

This is the most powerful version, but it provides the most problematic user experience. You run the kiosk in a virtual machine, you detect an uncaught exception - when you do you freeze the machine and save its state, and run a backup VM instead. I've only had to do this when tackling the most fearsome errors and it's not pretty. Unless you reallywant the whole captured state - don't do this.

这是最强大的版本,但它提供了最有问题的用户体验。你在虚拟机中运行 kiosk,你检测到一个未捕获的异常——当你冻结机器并保存它的状态时,运行一个备份虚拟机。我只在处理最可怕的错误时才需要这样做,这并不漂亮。除非你真的想要整个捕获的状态 - 不要这样做。

Really, do the extension before this - this is tedious but it gets very solid results.

真的,在此之前进行扩展 - 这很乏味,但会得到非常可靠的结果。



In conclusion, I think the first approach or even just the second one are more than enough for what you need. localStorageis an abstracted storage that web pages get for saving state without security issues. If that's not big enough we can talk about an IndexedDB solution.

总之,我认为第一种方法甚至只是第二种方法足以满足您的需要。localStorage是网页获得的抽象存储,用于在没有安全问题的情况下保存状态。如果这还不够大,我们可以谈谈 IndexedDB 解决方案。

It all really depends on the use case you have.

这完全取决于您拥有的用例。

回答by StaleMartyr

You can use XULRunner...a Mozilla runtime environment for XULapplications. It uses Geckolike Firefox and:

您可以使用XULRunner...一个用于XUL应用程序的 Mozilla 运行时环境。它像 Firefox 一样使用Gecko,并且:

  1. You can access the file system or using the SQLitedatabase to store logs.
  2. You can render your kiosk in fullscreen mode without using extensions.
  1. 您可以访问文件系统或使用SQLite数据库来存储日志。
  2. 您可以在不使用扩展程序的情况下以全屏模式呈现您的信息亭。

回答by Murph

Have you considered remote logging?

您是否考虑过远程日志记录?

I commonly assign window.onerrorto do send a request to a webserver storing the details of the error remotely. You could do the same with console.logif you preferred.

我通常指定window.onerror将请求发送到远程存储错误详细信息的网络服务器。console.log如果你愿意,你可以做同样的事情。

回答by Alan Dong

Try the following console export. It is a plugin for Firebugof Firefox. It's quite handy.

尝试以下控制台导出。它是Firefox 的Firebug插件。这很方便。

http://www.softwareishard.com/blog/consoleexport/

http://www.softwareihard.com/blog/consoleexport/

回答by SpliFF

If you are able/willing to switch from Firefox to Chrome or Opera you would be able to use the Sandboxed Filesystem API to write a local file. See:

如果您能够/愿意从 Firefox 切换到 Chrome 或 Opera,您将能够使用沙盒文件系统 API 来编写本地文件。看:

Start in kiosk mode using chrome.exe --kiosk <url>

使用自助服务终端模式启动 chrome.exe --kiosk <url>

You would then want to disable Alt-F4 and Ctrl-Alt-Del which on Windows can be done with several third-party tools like Auto Hotkey(Disable Ctrl-Alt-Del Script).

然后,您可能想要禁用 Alt-F4 和 Ctrl-Alt-Del,这在 Windows 上可以使用多个第三方工具完成,例如自动热键禁用 Ctrl-Alt-Del 脚本)。

回答by Reeno

You could use a remote logging script like Qbaka. It catches every JS error and sends it to the Qbaka server. There you can login and see all JS errors. Qbaka stores the exact error message, the script, line number, stack trace and the used browser for each error message.

您可以使用像Qbaka这样的远程日志记录脚本。它捕获每个 JS 错误并将其发送到 Qbaka 服务器。在那里您可以登录并查看所有 JS 错误。Qbaka 存储每个错误消息的确切错误消息、脚本、行号、堆栈跟踪和使用的浏览器。