Restartless Firefox Add-ons, Part 5: Logging

This is a simple one for most of you, but I found someone asking the question "How do I dump messages from bootstrap.js" on stackovercrap so I thought it would make a good topic to quickly blog about.

First of all what I'm about to describe is exactly how one would log messages and report errors from a JavaScript code module, so if you already know how to do this, then stop reading and bounce.

If you are lost without window.dump or don't know what that is, then give this page https://developer.mozilla.org/en/window.dump a quick read. There is a note that says:

"dump is commonly used to debug JavaScript. Privileged code can also use Components.utils.reportError and nsIConsoleService to log messages to the Error Console."

Bingo! Give the page on nsIConsoleService a re-read. There is a code example on the top of that page that you can use to get a console service, then the page describes it's various methods, such as logStringMessage. You can use the logStringMessage method in window.dump's stead.

The window.dump page also mentioned Components.utils.reportError which can be used for logging error messages to the Error Console.

The way that I prefer to access the console service is via the Services.jsm JavaScript code module, like so:

Components.utils.import("resource://gre/modules/Services.jsm");
Services.console.logStringMessage("this is a console message, look for it in the messages tab of the Error Console.");
© Erik Vold 2007-2013. Contact Erik Vold. Top ^