Skip to content

Instantly share code, notes, and snippets.

@andrewbrg
Created August 15, 2018 11:10
Show Gist options
  • Save andrewbrg/6df675b471460099720896a7d37aeb33 to your computer and use it in GitHub Desktop.
Save andrewbrg/6df675b471460099720896a7d37aeb33 to your computer and use it in GitHub Desktop.
Toggle console methods dynamically
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Allows the console browser object to be enabled or disabled seamlessly throughout your entire project. This is
// useful when you want to disable all console logging on a production release without removing the actual calls
// to the console.log (or other console methods) from your application code.
//
// Instructions:
// 1. Include this file before all other scripts in your project (including it first lets it capture all console methods)
// 2. Call the console.enabled() function passing a boolean indicating whether you want the console methods to be actioned or not
//
// By default this script will prevent console messages from appearing in the browser, so if you want them to be displayed
// make sure the console.enabled(true). You can also modify the flag at runtime in your browser's developer tools; again by
// calling console.enabled(true); to activate or calling console.enabled(false); to de-activate;
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
(function (c) {
var enabled_ = false;
var pAssert = c.assert;
var pClear = c.clear;
var pCount = c.count;
var pDebug = c.debug;
var pError = c.error;
var pInfo = c.info;
var pLog = c.log;
var pTable = c.table;
var pTrace = c.trace;
var pWarn = c.warn;
var pGroup = c.group;
var pGroupCollapsed = c.groupCollapsed;
var pGroupEnd = c.groupEnd;
var pTime = c.time;
var pTimeEnd = c.timeEnd;
var pDir = c.dir;
var pDirxml = c.dirxml;
var pProfile = c.profile;
var pProfileEnd = c.profileEnd;
var exec_ = function (fn, that, args) {
if (enabled_) {
return fn.apply(that, args);
}
};
console.log('%c Utilising console toggle ', 'background: #222; color: #FFFFFF');
console.enabled = function (status) {
enabled_ = (status === true);
if(enabled_) {
console.log('%c Console methods enabled ', 'background: #222; color: #4CA64C');
}
else {
console.log('%c Console methods disabled! ', 'background: #222; color: #DB3328');
}
};
console.assert = function () {
return exec_(pAssert, this, arguments);
};
console.clear = function () {
return exec_(pClear, this, arguments);
};
console.count = function () {
return exec_(pCount, this, arguments);
};
console.debug = function () {
return exec_(pDebug, this, arguments);
};
console.error = function () {
return exec_(pError, this, arguments);
};
console.info = function () {
return exec_(pInfo, this, arguments);
};
console.log = function () {
return exec_(pLog, this, arguments);
};
console.table = function () {
return exec_(pTable, this, arguments);
};
console.trace = function () {
return exec_(pTrace, this, arguments);
};
console.warn = function () {
return exec_(pWarn, this, arguments);
};
console.group = function () {
return exec_(pGroup, this, arguments);
};
console.groupCollapsed = function () {
return exec_(pGroupCollapsed, this, arguments);
};
console.groupEnd = function () {
return exec_(pGroupEnd, this, arguments);
};
console.time = function () {
return exec_(pTime, this, arguments);
};
console.timeEnd = function () {
return exec_(pTimeEnd, this, arguments);
};
console.dir = function () {
return exec_(pDir, this, arguments);
};
console.dirxml = function () {
return exec_(pDirxml, this, arguments);
};
console.profile = function () {
return exec_(pProfile, this, arguments);
};
console.profileEnd = function () {
return exec_(pProfileEnd, this, arguments);
};
})(console);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment