0

I was developing a small extension for Firefox. I wanted to log messages while a part of my extension is executing. CODE:

var aConsoleService = Components.classes["@mozilla.org/consoleservice;1"].getService (Components.interfaces.nsIConsoleService);
aConsoleService.logStringMessage("created"); 

Here "created" is message. But I am unable to see this message inside browser console. Am I missing something? I searched for it and got to know that you have to enable devtools.errorconsole.enabled inside about:config. I did that too. Please help me out.

Naman
  • 991
  • 2
  • 10
  • 20

1 Answers1

1

Are you sure you're opening the browser console? Ctrl + Shift + J?

var {utils:Cu, interfaces:Ci} = Components;
Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
consoleService.logStringMessage(text); 

also can try this:

var {utils:Cu, interfaces:Ci} = Components;
Cu.import('resource://gre/modules/Services.jsm');
Services.console.logStringMessage(text);

can also try this

var {utils:Cu, interfaces:Ci} = Components;
Cu.import('resource://gre/modules/Services.jsm');
Services.appShell.hiddenDOMWindow.console.log('blah');

if you're using addon sdk then instead of var {utils:Cu, interfaces:Ci} = Components; you have to do var {Cu, Ci} = require('chrome');

Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • I was using console from inspect element of browser. But ctrl+shift+j worked. Thanks. Web console vs browser console I guess. Does it log in side a file. – Naman Mar 03 '14 at 11:16
  • It won't log to a file so you'll have to implement that functionality separately. If you're targeting only recent versions of Firefox, you might be interested in the new Logging module: https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Log.jsm – Luckyrat Mar 03 '14 at 13:47
  • @Luckyrat for logging to file wouldn't [OSFile.jsm](https://developer.mozilla.org/en-US/docs/JavaScript_OS.File/OS.File_for_the_main_thread) be better? – Noitidart Mar 03 '14 at 16:51
  • @Noitidart sure, if you only have basic logging needs or want to target FF<27. But the logging module is supposed to provide a full logging framework - if I were writing logging into a new add-on today, I'd try that rather than write my own code around OSFile.jsm, consoleService, etc. I've not tried it yet but my understanding of log4js would be that you could attach a file appender using something like this: log.addAppender(new Log.FileAppender(new Log.BasicFormatter())); - the docs are very light at the moment though so definitely would take a little experimenting to find the right syntax. – Luckyrat Mar 03 '14 at 18:07
  • @Luckyrat oh wow no way, do you think with this we would be able to output the VariableViewer of an object to a file? This would be superb because I need to diff that so often. – Noitidart Mar 04 '14 at 05:26
  • I'm not familiar with the VariableViewer but the Log.jsm module looks flexible to support outputting any type of content, I'm just not sure how much extra work you'd need to put in to support a specific output format. Right now, I'm not working on anything that can be limited to FF 27+ but I'll play around with this module more when that does happen. – Luckyrat Mar 04 '14 at 13:07
  • @Luckyrat please try ```console.log('this is window:',window)``` then in browser console u will see a linik, please click it and variableViewer pops up, i was wondering if you can let me know if Log.jsm could dump that to file – Noitidart Mar 04 '14 at 13:16
  • I see - you're referring to part of the Firefox interface rather than a Firefox library or object. It sounds like you're after something like JSON.stringify() which you could then dump to a file or console but that doesn't work for complex objects. I think that finding a function to represent any object in a string format (with optional pretty-printing I guess) would be a separate challenge. Maybe something like that is already built in to Firefox, perhaps as part of the relatively new developer tools code that outputs the tree view in the VariableViewer panel. – Luckyrat Mar 07 '14 at 14:10
  • Stringify doe not get non-enumerables like var viewer does :( – Noitidart Mar 07 '14 at 16:53