Dev : Tips : Printing to JSConsole

From MozillaZine Knowledge Base
Revision as of 21:29, 11 July 2004 by Torisugari (talk | contribs) (naming convention)
Jump to navigationJump to search

You can output information to console using the following method:

  • Declare global variable gConsoleService:
var gConsoleService;
gConsoleService = Components.classes['@mozilla.org/consoleservice;1']
                   .getService(Components.interfaces.nsIConsoleService);

(note, you can also use toJavaScriptConsole() function to open JavaScript console on startup)

  • Define a function:
function _MyApp_logMessage(message) {
  gConsoleService.logStringMessage('MyApp: ' + message);
}
  • Use it like here:
_MyApp_logMessage('Initialized.');

(thanks to clav, velcrospud and sage.mozdev.org for this)

See also: Using dump()