20

I understand that I can log to the console when using a Firefox extension with the cfx run command.

Is there a way to log to a console after the extension has been packaged with cfx xpi? Logging to Firebug is fine if possible.

I have found two blog posts about this (here and here). Both are rather old and don't work any more.

I'm using version 1.10 of the add-on SDK and FF15.

czerny
  • 15,090
  • 14
  • 68
  • 96
David Tuite
  • 22,258
  • 25
  • 106
  • 176

5 Answers5

33

You need to do 2 things:

  1. enable logging for addons

• in about:config, add a new option extensions.sdk.console.logLevel and give it the value "all"

• restart Firefox

  1. in Firefox open the Browser Console:

• Tools -> Web developer -> Browser console

• NOTE: this is different from the usual Web Console used to debug web pages

You should see addons logs there now.

mindrones
  • 1,173
  • 11
  • 15
  • 2
    Shortcut for browser console is Ctrl+Shift+J – Gaurang Tandon Jan 16 '16 at 03:59
  • 3
    In addition, you need to enable the option "Show Content Messages" from within the Browser Console. – ojchase Jan 27 '21 at 06:13
  • I'm using Firefox 91 and those 2 things are still true and needed, but I would add that the option `extensions.sdk.console.logLevel` was not pre-existing in my config as I was assuming, which slightly confused me. – cjauvin Sep 07 '21 at 14:32
  • This needed an update - I posted an update in this thread. – devyJava Dec 03 '21 at 03:04
7

TLDR:

  1. Go to about:config url and create key extensions.sdk.console.logLevel with value all
  2. See log messages either in Browser console (Ctrl+Shift+J) or in terminal you started Firefox from.

cfx or its successor jpm creates this configuration key automatically in development firefox profile.

From logging documentation:

extensions.sdk.console.logLevel: if set, this determines the logging level for all installed SDK-based add-ons.

extensions.extensionID.sdk.console.logLevel, where extensionID is an add-on's Program ID. If set, this determines the logging level for the specified add-on. This overrides the global preference if both preferences are set.

czerny
  • 15,090
  • 14
  • 68
  • 96
  • As mentioned by @ojchase under another answer, you also need to enable "Show Content Messages" in the Browser Console settings. – joriki Nov 21 '21 at 08:09
2

I will summarize recent changes that has taken with Firefox since this question got posted -- basically updated @LucaBonavita answer.

  1. In about:config, check if option extensions.sdk.console.logLevel is enabled. If not, toggle to enable. You might need to create it if it does not exist

  2. Open Browser Console

  • Menu Bar -> Tools -> Browser Tools -> Browser Console
  • Enable Show Content Messages in browser console.

enter image description here

Now, console.log triggered by Firefox addon/extension should display

Tested on Firefox 94.0.2 (64-bit) and 95.0b12 (64-bit)

devyJava
  • 135
  • 8
0

Have you tried console.log() with Firebug? (I know you have tried Application.console.log() and Firebug.console.log() already)

I just tried it in the Web Console on FF16 running Firebug 1.10.4 and it seems to work: Firefox Web Console

Here are some more examples from the Firebug Wiki itself: FirebugWiki Console API

Additionally, you can write messages of different types in the console, such as: console.debug(), console.info(), console.warn(), console.error()

Aamir
  • 5,324
  • 2
  • 30
  • 47
  • 3
    You're not doing this from an add-on though right? I can `console.log()` from the console itself (or from a web page) no problem. The issue is accessing the console from within the context of an add-on. – David Tuite Oct 15 '12 at 12:06
  • @DavidTuite: Sorry, I missed that part. Have you tried the suggestions in [this thread](http://stackoverflow.com/questions/1182816/how-do-you-log-to-firebug-from-an-extension) already? – Aamir Oct 15 '12 at 12:12
  • That seems to just suggest using the `Firebug` object like one of the blog posts I linked to did. I don't seem to be able to access that even though I have Firebug installed. – David Tuite Oct 15 '12 at 12:34
  • @DavidTuite: That's expected - Firebug is absolutely useless for extensions, it's meant for web pages only. – Wladimir Palant Oct 17 '12 at 05:25
  • I tried `console.log` but it didn't seem to work, but `Application.console.log` worked. – Tianyang Li Jul 12 '13 at 15:28
-1

If you run console.log from Add-on code, it send up in the 'Messages' tab of the Error Console window:

https://www.evernote.com/shard/s1/sh/c40c509d-9f14-4103-920b-4186a426226c/53572f96a3bbe0462103a8126b8e9822/res/fcbead60-d9d0-48be-b744-3fa6701fdf1b/skitch.png

Makyen
  • 31,849
  • 12
  • 86
  • 121
therealjeffg
  • 5,790
  • 1
  • 23
  • 24