Skip to content

Instantly share code, notes, and snippets.

@allenhwkim
Created October 7, 2021 18:51
Show Gist options
  • Save allenhwkim/a1e94fd0deb1f974bc705cd9328c0a30 to your computer and use it in GitHub Desktop.
Save allenhwkim/a1e94fd0deb1f974bc705cd9328c0a30 to your computer and use it in GitHub Desktop.
konsole, console wrapper with log level
export const konsole = {
LOG_LEVEL: 'log',
LOG_LEVELS:
{ all: 0, debug: 1, log: 2, info: 3, warn: 4, error: 5, none: 6 },
debug: function() { konsole.write('debug', arguments) },
info: function() { konsole.write('info', arguments) },
log: function() { konsole.write('log', arguments) },
warn: function() { konsole.write('warn', arguments) },
error: function() { konsole.write('error', arguments) },
setLevel: function(level) { konsole.LOG_LEVEL = level},
write: function(funcName, args) {
const restrictedLevel = konsole.LOG_LEVELS[konsole.LOG_LEVEL];
const requiredLevel = konsole.LOG_LEVELS[funcName];
if (restrictedLevel <= requiredLevel) {
console[funcName].apply(console, args);
return true;
}
}
}
// test, only yes should show up
// konsole.setLevel('all');
// konsole.debug('all debug', 'yes');
// konsole.log('all log', 'yes');
// konsole.info('all info', 'yes');
// konsole.warn('all warn', 'yes');
// konsole.error('all error', 'yes');
// konsole.setLevel('info');
// konsole.debug('info debug', 'no');
// konsole.log('info log','no');
// konsole.info('info info', 'yes');
// konsole.warn('info warn', 'yes');
// konsole.error('info error', 'yes');
// konsole.setLevel('warn');
// konsole.debug('warn debug', 'no');
// konsole.log('warn log', 'no');
// konsole.info('warn info', 'no');
// konsole.warn('warn warn', 'yes');
// konsole.error('warn error', 'yes');
// konsole.setLevel('error');
// konsole.debug('error debug', 'no');
// konsole.log('error log', 'no');
// konsole.info('error info', 'no');
// konsole.warn('error warn', 'no');
// konsole.error('error error', 'yes');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment