Provide an alternative to console.log which checks a debug flag before logging



7 years ago
6 years ago


(Reporter: kmag, Assigned: myk)


Firefox Tracking Flags

(Not tracked)



(1 attachment)



7 years ago
One of the most common reasons I'm withholding full review from Jetpack add-ons lately is that they tend to produce large amounts of debugging Error Console output. I think that a prominent API with builtin consideration for debugging flags would go a long way towards making authors conscious of the need to stem their debugging output in release versions.

The flag should probably be stored as a preference which defaults to false. As to how authors should enable it in their own builds and setups, I have no idea.
There actually are already a bunch of methods on the console object: including a console.debug(). I think that might only do stuff in actual debug builds of Firefox, though.

Comment 2

7 years ago
If the debug method suits the purposes of this bug then a documentation update might be sufficient. I think, given the complexity of the console API, that a parallel 'debug' object with the same message might be a better route. I.e., debug.log(), debug.trace()

Comment 3

7 years ago
The conventional solution to this problem is to specify a default log level for production environments that is stricter than that for testing environments.  For example, testing environments might log all messages that are at the "info" level of severity or higher by default, while production environments log all messages that are at the "warn" or "error" level of severity.

The Jetpack console object supports the following four levels of severity and corresponding methods for logging at those levels:

* debug: debug()
* info: info(), log()
* warning: warn()
* error: error()

We should set the default log level to "info" for testing environments, i.e. when running automated tests for an addon via `cfx test` and testing the addon manually via `cfx run`; and "error" for production environments, i.e. when using an addon by installing it from a package created by `cfx xpi`.

We should also allow users to specify a log level, so developers can choose to see "debug" messages while debugging their addons and users can choose to see less severe messages while gathering information to report a bug.  But that can be done in a separate bug.
Priority: -- → P2
Blocks: 713046

Comment 4

6 years ago
Created attachment 692503 [details]
pull request 689 - set a default log level and add prefs for changing it

This adds a default log level to the plain-text-console module along with a mechanism to change the log level for a specific addon or all SDK-based addons.

The log levels, and the console methods they enable, are:

    all: all methods
    debug: debug(), log(), info(), warn(), error()
    info: log(), info(), warn(), error()
    warn: warn(), error()
    error: error()
    off: no methods

The names of these levels are taken from log4javascript, which is based on log4j. The default log level for cfx run is info; the default level for an installed addon is error.

The only (cosmetic) change to existing functionality is that warn() messages now get prefixed with warn instead of warning for consistency with the name of the method and the name of the log level.

I intentionally placed the addon-specific preference inside the extensions.[id] namespace, so addons can create a simple pref for it. I also intentionally placed it inside an sdk subnamespace, i.e. extensions.[id].sdk, to isolate it from an addon's own preferences. This establishes a precedent for other such prefs, should we choose to add some in the future.
Assignee: nobody → myk
Attachment #692503 - Flags: review?(rFobic)
Comment on attachment 692503 [details]
pull request 689 - set a default log level and add prefs for changing it

That's really cool Myk, thanks!! Could you please hold on landing it though, tree is orange now, I'll land this as soon as we'll make it green again.
Attachment #692503 - Flags: review?(rFobic) → review+

Comment 6

6 years ago
Sure thing, I'll let you land it at your convenience!

Comment 7

6 years ago
Commit pushed to master at
fix Bug 711815 - set a default log level and add prefs for changing it r=@gozala


6 years ago
Last Resolved: 6 years ago
Resolution: --- → FIXED

Comment 8

6 years ago
Commit pushed to integration at
fix Bug 711815 - set a default log level and add prefs for changing it r=@gozala
You need to log in before you can comment on or make changes to this bug.