Conditional Logging: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
Created page with 'Provide a single easy switch to control whether debugging information is output. == Alternative 1 == <pre class='sample'> const DEBUG = true; function debug() { if (DEBUG &&…'
 
No edit summary
Line 38: Line 38:
An even simpler option, retaining all of Firebug's console routines, and not interfering with additional parameters.
An even simpler option, retaining all of Firebug's console routines, and not interfering with additional parameters.


<pre class='sample-good'>
<pre class='sample'>
const DEBUG = 1;
const DEBUG = 1;
if (!DEBUG) {
if (!DEBUG) {
Line 48: Line 48:
   };
   };
}
}
</pre>
== Alternative 4 ==
THE SIMPLEST option, so simple that it's actually kinda more complicated...
But it encapsulates all of Firebug's functionality.
<pre class='sample-good'>
const DEBUG = 1;
if (!console) console = function() {
  var console = {},
    c = unsafeWindow.console,
    cfn = ['log','debug','info','warn','error','assert','dir','dirxml','trace','group','groupCollapsed','groupEnd','time','timeEnd','profile','profileEnd','count','exception','table']; 
  for(var i=0,l=cfn.length; i<l; i++) {
    console[cfn[i]] = function() {
      if (DEBUG && c)
        c[cfn[i]].apply(null, arguments)
    }
  }
  return console;
}();
</pre>
</pre>


[[Category:Coding Tips]]
[[Category:Coding Tips]]

Revision as of 04:11, 3 July 2010

Provide a single easy switch to control whether debugging information is output.

Alternative 1

const DEBUG = true;

function debug() {
  if (DEBUG && console) {
    console.log.apply(this, arguments);
  }
}
var links = document.links;
debug("Links: %o", links);

Alternative 2

A slightly simpler method that lets you use more of the Firebug console's routines.

const DEBUG = 1;

var _orig_console = console;
var console = {
  log: function (text) { if (DEBUG) _orig_console.log(text); },
  info: function (text) { if (DEBUG) _orig_console.info(text); },
  warn: function (text) { if (DEBUG) _orig_console.warn(text); },
  error: function (text) { if (DEBUG) _orig_console.error(text); }
};

Alternative 3

An even simpler option, retaining all of Firebug's console routines, and not interfering with additional parameters.

const DEBUG = 1;
if (!DEBUG) {
  var console = {
    log: function() {},
    info: function() {},
    warn: function() {},
    error: function() {},
  };
}

Alternative 4

THE SIMPLEST option, so simple that it's actually kinda more complicated...

But it encapsulates all of Firebug's functionality.

const DEBUG = 1;
if (!console) console = function() {
  var console = {},
    c = unsafeWindow.console,
    cfn = ['log','debug','info','warn','error','assert','dir','dirxml','trace','group','groupCollapsed','groupEnd','time','timeEnd','profile','profileEnd','count','exception','table'];  
  for(var i=0,l=cfn.length; i<l; i++) {
    console[cfn[i]] = function() {
      if (DEBUG && c)
        c[cfn[i]].apply(null, arguments)
    }
  }
  return console;
}();