Skip to content

Instantly share code, notes, and snippets.

@FranciscoG
Last active June 27, 2018 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FranciscoG/483f5127601e21c8d3f5fe2e97e1cd30 to your computer and use it in GitHub Desktop.
Save FranciscoG/483f5127601e21c8d3f5fe2e97e1cd30 to your computer and use it in GitHub Desktop.
wrapper around all the console methods so you can turn enable/disable them.
(function(window){
"use strict";
/**
* Wrap console methods to turn them on/off with a flag
* Plus stub console methods for older browsers without console
*
* @param {boolean} debugFlag
*/
function Logger(debugFlag){
this.debug = debugFlag || false;
}
// borrowed from https://github.com/h5bp/html5-boilerplate/blob/master/dist/js/plugins.js
var method;
var noop = function () {};
var methods = [
'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
'timeline', 'timelineEnd', 'timeStamp', 'trace', 'warn'
];
var length = methods.length;
var console = (window.console = window.console || {});
while (length--) {
method = methods[length];
// Only stub undefined methods.
if (!console[method]) {
console[method] = noop;
}
// setup our Logger wrapped methods as well
Logger.prototype[method] = new Function(
'if (this.debug){console.'+method+'.apply(console, arguments);}'
)
Logger.prototype[method].bind(Logger);
}
// example usage:
window.logger = new Logger(/* pass a bool here, I use ?debug and read location.search on page load */);
})(window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment