Created
March 6, 2014 16:20
-
-
Save fliptopbox/9393310 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is a proxy function to minimize console.log | |
output on production server. It creates non-existant | |
window.console Object for old browsers, and defers all | |
log events via the proxy function. | |
Usage: | |
(on your dev server) | |
window.message('kilroy was here...'); // normal output. | |
(on your production server) | |
window.message('kilroy was here...'); // no output. silence. | |
Bypassing the silence: | |
console.log('bypass', 1, [2 ,3, 4]); // 1st argument 'bypass' will activate console.logs | |
http://blahblah.com/#debug // #debug will activate console.logs | |
*/ | |
window.message = (function () { | |
/* This is a proxy for console.log */ | |
var isProduction = true, // add your regex here | |
bypassFlag = null, | |
prefix = '[APP]', | |
fn = function () { return; }; | |
if (!window.console || !window.console.log) { | |
window.console = { log: fn, debug: fn, info: fn, warn: fn, error: fn }; | |
} | |
return function (/* arguments */) { | |
var bypass = (/^#debug$/gi).test(window.location.hash), | |
args = Array.prototype.slice.call(arguments) || []; | |
bypassFlag = bypassFlag || (args.length && args[0] === 'bypass') || bypass || false; | |
args.splice(0, 0, prefix); | |
if (!bypassFlag && isProduction) { return; } | |
try { | |
window.console.log.apply(window.console, args); | |
} catch (err) { | |
return; | |
} | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment