<script src="bower_components/dist/logeek.js"></script>
logeek.show('security');
logeek('some message').at('copy'); //this won't be logged
logeek('other message').at('secturity'); //this would be logged
//Make a copy of the old console.
var oldConsole = Object.assign({}, console);
//This function redefine the caller with the original one. (well, at least i expect this to work in chrome, not tested in others)
function setEnabled(bool) {
if (bool) {
//Rewrites the disable function with the original one.
console[this.name] = oldConsole[this.name];
//Make sure the setEnable will be callable from original one.
console[this.name].setEnabled = setEnabled;
} else {
//Rewrites the original.
var fn = function () {/*function disabled, to enable call console.fn.setEnabled(true)*/};
//Defines the name, to remember.
Object.defineProperty(fn, "name", {value: this.name});
//replace the original with the empty one.
console[this.name] = fn;
//set the enable function
console[this.name].setEnabled = setEnabled
}
}
var FLAGS = {};
FLAGS.DEBUG = true;
FLAGS.INFO = false;
FLAGS.LOG = false;
//Adding dir, table, or other would put the setEnabled on the respective console functions.
function makeThemSwitchable(opt) {
var keysArr = Object.keys(opt);
//its better use this type of for.
for (var x = 0; x < keysArr.length; x++) {
var key = keysArr[x];
var lowerKey = key.toLowerCase();
//Only if the key exists
if (console[lowerKey]) {
//define the function
console[lowerKey].setEnabled = setEnabled;
//Make it enabled/disabled by key.
console[lowerKey].setEnabled(opt[key]);
}
}
}
//Put the set enabled function on the original console using the defined flags and set them.
makeThemSwitchable(FLAGS);
Or, you could have a variable in your code if you like, but above is better.
Restart cmd/ command line, or, IDE/ editor like Netbeans
Have below like code:
console.debug = console.log; // define debug function
console.silly = console.log; // define silly function
switch (process.env.LOG_LEVEL) {
case 'debug':
case 'silly':
// print everything
break;
case 'dir':
case 'log':
console.debug = function () {};
console.silly = function () {};
break;
case 'info':
console.debug = function () {};
console.silly = function () {};
console.dir = function () {};
console.log = function () {};
break;
case 'trace': // similar to error, both may print stack trace/ frames
case 'warn': // since warn() function is an alias for error()
case 'error':
console.debug = function () {};
console.silly = function () {};
console.dir = function () {};
console.log = function () {};
console.info = function () {};
break;
}
Now use all console.* as below:
console.error(' this is a error message '); // will print
console.warn(' this is a warn message '); // will print
console.trace(' this is a trace message '); // will print
console.info(' this is a info message '); // will print, LOG_LEVEL is set to this
console.log(' this is a log message '); // will NOT print
console.dir(' this is a dir message '); // will NOT print
console.silly(' this is a silly message '); // will NOT print
console.debug(' this is a debug message '); // will NOT print
Now, based on your LOG_LEVEL settings made in the point 1 (like, setx LOG_LEVEL log and restart command line), some of the above will print, others won't print
const testArray = {
a: 1,
b: 2
};
const verbose = true; //change this to false to turn off all comments
const consoleLog = (...message) => {
return verbose ? console.log(...message) : null;
};
console.log("from console.log", testArray);
consoleLog("from consoleLog", testArray);
// use consoleLog() for the comments you want to be able to toggle.
// old console to restore functionality
const consoleHolder = window.console;
// arbitrary strings, for which the console stays on (files which you aim to debug)
const debuggedHandlers = ["someScript", "anotherScript"];
// get console methods and create a dummy with all of them empty
const consoleMethodKeys = Object.getOwnPropertyNames(window.console).filter(item => typeof window.console[item] === 'function');
const consoleDummy = {};
consoleMethodKeys.forEach(method => consoleDummy[method] = () => {});
export function enableConsoleRedirect(handler) {
if (!debuggedHandlers.includes(handler)) {
window.console = consoleDummy;
}
}
export function disableConsoleRedirect() {
window.console = consoleHolder;
}
let console_disabled = true;
function console_log() {
if (!console_disabled) {
console.log.apply(console, arguments); //Dev Tools will mark the coding line here
}
}
但是,这将创建另一个函数,并且您无法使用Dev Tools中显示的编码行记录消息。
这就是2022年之路。
// Disable Console Log without altering debug coding line.
// No override original `console` object
let console_disabled = false;
const nullFunc = function(){};
const _console = new Proxy(console, {
get(target, prop, receiver){
if(prop==='log' && console_disabled){
return nullFunc;
}
return Reflect.get(...arguments)
}
});
console_disabled = true;
_console.log('you cannot see me');
console_disabled = false;
_console.log('you can see me @ line 18');
console_disabled = true;
_console.log('you cannot see me');
console_disabled = false;
_console.log('you can see me @ line 22');
你也可以重写原来的console对象。
// Disable Console Log without altering debug coding line.
// Override original `console` object
let console_disabled = false;
const nullFunc = function(){};
console = new Proxy(console, {
get(target, prop, receiver){
if(prop==='log' && console_disabled){
return nullFunc;
}
return Reflect.get(...arguments)
}
});
console_disabled = true;
console.log('you cannot see me');
console_disabled = false;
console.log('you can see me @ line 18');
console_disabled = true;
console.log('you cannot see me');
console_disabled = false;
console.log('you can see me @ line 22');