最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Log JavaScript console into a log file with Firefox - Stack Overflow

programmeradmin2浏览0评论

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.

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.

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.

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.

Share Improve this question edited Apr 17, 2017 at 11:15 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Oct 10, 2013 at 14:22 NagyINagyI 5,9978 gold badges58 silver badges86 bronze badges 8
  • You could use the file api and write to the local file system. – epascarello Commented Oct 10, 2013 at 14:29
  • Cab you target indexedDb with your logger instead of the server? – Mister Epic Commented Oct 10, 2013 at 14:29
  • 1 have you tried softwareishard.com/blog/consoleexport yet? – Zathrus Writer Commented Oct 10, 2013 at 14:35
  • Not exactly what you're looking for, just an idea that I tried out before: use window.onerror and save all errors to localStorage. – user123444555621 Commented Oct 10, 2013 at 14:37
  • 2 @NagyI It sounds like you need a browser plugin which will take care of this. I tried searching for one but couldn't find any. Sounds like an interesting weekend project. – Munim Commented Oct 15, 2013 at 8:48
 |  Show 3 more comments

7 Answers 7

Reset to default 4 +25

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.

From what I understand your issue is

I'd like to make Firefox log all errors

There are several approaches we can do to tackle this

First approach - log everything to localStorage too:

Now, rather than writing to an actual file, you can write to localStorage or IndexedDB instead.

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.

Second approach - log only errors

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

// 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 really want the whole captured state - don't do this.

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. localStorage is 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.

It all really depends on the use case you have.

You can use XULRunner...a Mozilla runtime environment for XUL applications. It uses Gecko like Firefox and:

  1. You can access the file system or using the SQLite database to store logs.
  2. You can render your kiosk in fullscreen mode without using extensions.

Have you tried jserrorcollector? We are using it and it works fine (only in Firefox). It's only for 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

Have you considered remote logging?

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

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

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

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:

  • http://www.html5rocks.com/en/tutorials/file/filesystem/
  • http://caniuse.com/filesystem

Start in kiosk mode using 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).

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.

发布评论

评论列表(0)

  1. 暂无评论