Login Register

Can you extend console.debug or console?

Right now when I'm running our application I check djConfig.isDebug using a global function and then depending on the boolean returned I console.debug messages out. Is there a way to extend console.debug or console to not fire out messages unless isDebug is true?

Hrmmm

Hey 2dor:

First, perhaps it should be noted that when isDebug is false, we define stub functions for the console.* methods if they're not already defined. This isn't the same as "not logging" if you have Firebug installed, but for all users who don't have firebug, the effect is exactly that: logging statements have no effect. This means that when isDebug == false your users probably aren't paying a noticeable price for your logging statements. Only users with firebug installed and enabled can see them in that case.

That said, I understand (and have often thought about) the difference between calls to no-ops and not doing anything at all. The biggest problem that all logging frameworks face is that if the language they're written in doesn't support macros, your options are either to pass functions and not strings (which implies the expense of closure creation no matter what) or to recommend that users surround all logging statements with if blocks as you're doing now in order to *completely* eliminate the string allocation/function-call overhead of passing strings and objects into those no-op functions.

So Dojo has a couple of options:

  • Define a "shadow the console methods no matter what" config flag. I'm not personally in favor of this because I often need to debug live apps, and I suspect you probably will need to as well.
  • Define alternate dojo.* methods which shadow the console methods and have the same effect but are no-ops when isDebug is false. Again, I'm not in favor but this time it's due to the added "WTF?" nature of Dojo then defining two parallel (and in most cases equivalent) sets of logging/debugging methods, particularly when it's not clear that we could add value over the firebug methods. Firebug kicks that much butt.
  • Define a console.conditionalLog() or similar. I don't have a strong opinion about this one.
  • Provide a build-tool step to strip console.* calls of all types. This seems smart to me.

What do you think?

Regards

--
Project Lead, The Dojo Toolkit
President, The Dojo Foundation

BTW, there is a trac ticket

BTW, there is a trac ticket in for stripping console.* calls from the source as part of a build step. It is scheduled for Dojo 1.2 at the moment:
http://trac.dojotoolkit.org/ticket/1368