Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Console output manager
/**
* @author Jacques Favreau (@betaorbust)
* @fileOverview Console manager.
* The utility will silence all console output (info, log, error) unless a dev cookie is set.
* Does the cookie/silence check on load, and also via the exported methods for turning it on/off
* after pageload.
* Exports a global consoleManager object with the following methods:
* silence(): Shuts the console up.
* restore(): Gets the console all chatty.
* toggle(): toggle the dev cookie state and does an appropriate silence or restore.
*
* See console_manager.min.js for a < 1kb minified version of this file.
*/
(function(window, document, undefined) {
var con = window.console; // convenience method
var COOKIENAME = 'bringthenoise';
var COOKIE_VALUE = 'true';
var FULL_COOKIE_STRING=COOKIENAME+'='+COOKIE_VALUE;
// Store the original system functions
var oldConsole = {
info: window.console.info,
log: window.console.log,
error: window.console.error
};
// Doesn't do shit.
var noOp = function(){};
// Quick lib for dealing with our cookie.
window.cookieLib = {
hasCookie: function () {
return document.cookie.indexOf(FULL_COOKIE_STRING) >= 0;
},
writeCookie: function() {
document.cookie = FULL_COOKIE_STRING;
},
removeCookie: function () {
if (cookieLib.hasCookie()) {
document.cookie = COOKIENAME + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
};
// What we'll expose on the object we'll make global
var exported = {
silence: function(){ // Shut up.
window.console.info = noOp;
window.console.log = noOp;
window.console.error = noOp;
cookieLib.removeCookie();
},
restore: function(){ // Get all talky.
window.console.info = oldConsole.info;
window.console.log = oldConsole.log;
window.console.error = oldConsole.error;
cookieLib.writeCookie();
},
toggle: function(){ // Do the other one ;)
return cookieLib.hasCookie() ? exported.silence() : exported.restore();
}
};
// Do the runtime configuration.
// If the cookie isn't set, turn off all console output.
if(!cookieLib.hasCookie()){
exported.silence();
}
// Actually export our stuff
window.consoleManager = exported;
})(window, document);
(function(a,c,undefined){function d(){}var e=a.console.info,f=a.console.log,g=a.console.error;a.f={a:function(){return 0<=c.cookie.indexOf("bringthenoise=true")},d:function(){c.cookie="bringthenoise=true"},c:function(){cookieLib.a()&&(c.cookie="bringthenoise=; expires=Thu, 01 Jan 1970 00:00:00 GMT")}};var b={b:function(){a.console.info=d;a.console.log=d;a.console.error=d;cookieLib.c()},restore:function(){a.console.info=e;a.console.log=f;a.console.error=g;cookieLib.d()},toggle:function(){return cookieLib.a()?
b.b():b.restore()}};cookieLib.a()||b.b();a.e=b})(window,document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment