Skip to content

Instantly share code, notes, and snippets.

@jpiccari
Created July 29, 2014 04:53
Show Gist options
  • Save jpiccari/dbb15d1a1badf007e87e to your computer and use it in GitHub Desktop.
Save jpiccari/dbb15d1a1badf007e87e to your computer and use it in GitHub Desktop.
Simple console module that allows mimics the Console API but which can be toggled to help keep your console clean when not debugging.
define(
'console',
function() {
var QUEUED_API = [
'count',
'dir',
'error',
'group',
'groupCollapsed',
'groupEnd',
'info',
'log',
'table',
'warn'
],
windowConsole = window.console,
queue = [],
isEnabled,
method;
function console(type /* ... */) {
var args = Array.prototype.slice.call(arguments, 1);
if (isEnabled) {
windowConsole[type].apply(windowConsole, args);
} else if(QUEUED_API.indexOf(type) !== -1) {
queue.push({
type: type,
args: args
});
}
}
for (method in windowConsole) {
if (typeof windowConsole[method] === 'function') {
console[method] = console.bind(0, method);
}
}
console.enable = function() {
var log;
isEnabled = true;
Error.stackTraceLimit = Infinity;
while ((log = queue.shift())) {
windowConsole[log.type].apply(windowConsole, log.args);
}
};
console.disable = function() {
isEnabled = false;
};
return console;
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment