Firefox extension logging
Last week, I was tasked at work to create a quick prototype extension. This machine had a few other extensions, including the very verbose SmartSwipe extension. SmartSwipe extension logs almost everything to the error console as messages. Which in turns makes it a pretty bad choice for any other extension who wants to use the error console as a logging service (incidentally, I am the main developer of the SmartSwipe extension, so this is entirely my fault).
Despite Firefox extensions growing more complex, there are two areas of extension development that are still a pain to deal with: debugging and logging.
What pains me is that extensions have a pretty crappy choice of methods to use for logging purposes. There is dump(), Components.utils.reportError(), FUEL’s Application.console.log(), and maybe some others I am missing (ChromeBug is another possibility, but I haven’t used it). But these methods of logging don’t scale very well. If you have more than 1 extension using the same logging service, you introduce noise that can be difficult to sort through.
It striked me that there was nothing simple to capture and filter your logging information. Since this problem was annoying me, I decided to create an extension to deal with this.
Introducing Debug Log
The extension is uncreatively called Debug Log (unrelated to Jeremy Gillick’s DebugLogger). It shows a Windows-style event viewer with basic filtering. To use it is really easy. First I will show how it is used, and next how to use it when the Debug Log extension isn’t installed:
var log = {};
(function() {
var modules = {};
Components.utils.import("chrome://debuglog/content/DebugLog.jsm",
modules);
log = new modules.DebugLog("my_extension_slug_name");
log.info("Hello")
log.warn("Trimming to 8 characters. String : " + s);
log.error("MyTerribleFunction", exception);
log.assert(foo != null, "foo should never be null");
})();
Which results in the following:

Assert is a bit different from typical assertion in other languages. It will not quit the application, nor would it throw an exception (unless you passed a wrong parameter to assert).
To use it properly, you must also account for times when DebugLog is not installed.
var logging = {};
(function() {
try
{
var modules = {};
Components.utils.import("chrome://debuglog/content/DebugLog.jsm",
modules);
logging = new modules.DebugLog("JavascriptPlusPlus");
}
catch (e)
{
logging.assert = logging.warn = logging.info = logging.error = function() {}
}
})()
logging.info("Starting...");
The extension still has a lot of work to be done, but it’s useable now. So now is the time to release and it is available at https://addons.mozilla.org/en-US/firefox/addon/246799/. Enjoy!



