Last active
April 19, 2018 20:14
-
-
Save allenhwkim/ceebbfa66771be0b8aedadd73e715d7b to your computer and use it in GitHub Desktop.
Custom console, https://stackblitz.com/edit/angular-konsole
This file contains hidden or 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
var konsole = { | |
LOG_LEVELS: { | |
ALL: parseInt('11111',2), | |
DEBUG: parseInt('00001',2), | |
LOG: parseInt('00010',2), | |
INFO: parseInt('00100',2), | |
WARN: parseInt('01000',2), | |
ERROR: parseInt('10000',2), | |
NONE: parseInt('00000',2) | |
}, | |
LOG_LEVEL: 'LOG', | |
toLog: function(param) { | |
return this.LOG_LEVELS[this.LOG_LEVEL] >= this.LOG_LEVELS[param]; | |
}, | |
debug: function() { | |
this.toLog('DEBUG') && console.debug.apply(console, arguments); | |
}, | |
log: function() { | |
this.toLog('LOG') && console.log.apply(console, arguments); | |
}, | |
info: function() { | |
this.toLog('INFO') && console.info.apply(console, arguments); | |
}, | |
warn: function() { | |
this.toLog('WARN') && console.warn.apply(console, arguments); | |
}, | |
error: function() { | |
this.toLog('ERROR') && console.error.apply(console, arguments); | |
}, | |
}; | |
// konsole.LOG_LEVEL='LOG'; // default | |
// konsole.error('error', 1, 2, 3); // no | |
// konsole.log('log', 1,2,3,4,5); // yes | |
// konsole.LOG_LEVEL='ERROR'; | |
// konsole.error('error', 1, 2, 3); // yes | |
// konsole.log('log', 1,2,3,4,5); // yes |
This file contains hidden or 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
// https://stackblitz.com/edit/angular-konsole | |
export abstract class konsole { | |
static LOG_LEVELS = { | |
ALL: parseInt('00000', 2), | |
DEBUG: parseInt('00001', 2), | |
LOG: parseInt('00010', 2), | |
INFO: parseInt('00100', 2), | |
WARN: parseInt('01000', 2), | |
ERROR: parseInt('10000', 2), | |
NONE: parseInt('11111', 2) | |
}; | |
static logLevel: string = 'INFO'; | |
static toLog = function(param) { // returns to log or not | |
const restrictionNum = this.LOG_LEVELS[this.logLevel]; | |
const requiredNum = this.LOG_LEVELS[param]; | |
return requiredNum > restrictionNum; | |
} | |
public static setLogLevel(logLevel) { | |
logLevel = logLevel.toUpperCase(); | |
const logLevels = Object.keys(this.LOG_LEVELS); | |
if (logLevels.indexOf(logLevel) > -1) { | |
if (window && window.sessionStorage) { // for browser env. | |
window.sessionStorage.setItem('konsole.LOG_LEVEL', logLevel); | |
} | |
this.logLevel = logLevel; | |
} else { | |
console.error(`Error, invalid logLevel, it must be one of ${logLevels}`); | |
} | |
} | |
public static getLogLevel() { | |
if (window && window.sessionStorage) { // for browser env. | |
return window.sessionStorage.getItem('konsole.LOG_LEVEL'); | |
} else { | |
return this.logLevel; | |
} | |
} | |
public static debug(...args: any[]) { | |
this.toLog('DEBUG') && console.debug.apply(console, arguments); | |
} | |
public static log(...args: any[]) { | |
this.toLog('LOG') && console.log.apply(console, arguments); | |
} | |
public static info(...args: any[]) { | |
this.toLog('INFO') && console.info.apply(console, arguments); | |
} | |
public static warn(...args: any[]) { | |
this.toLog('WARN') && console.warn.apply(console, arguments); | |
} | |
public static error(...args: any[]) { | |
this.toLog('ERROR') && console.error.apply(console, arguments); | |
} | |
} | |
// konsole.setLogLevel('all'); | |
// konsole.debug('yes'); | |
// konsole.log('yes'); | |
// konsole.info('yes'); | |
// konsole.warn('yes'); | |
// konsole.error('yes'); | |
// konsole.setLogLevel('none'); | |
// konsole.debug('no'); | |
// konsole.log('no'); | |
// konsole.info('no'); | |
// konsole.warn('no'); | |
// konsole.error('no'); | |
// konsole.setLogLevel('info'); | |
// konsole.debug('no'); | |
// konsole.log('no'); | |
// konsole.info('yes'); | |
// konsole.warn('yes'); | |
// konsole.error('yes'); | |
// konsole.setLogLevel('WARN'); | |
// konsole.debug('no'); | |
// konsole.log('no'); | |
// konsole.info('no'); | |
// konsole.warn('yes'); | |
// konsole.error('yes'); | |
// konsole.setLogLevel('ERROR'); | |
// konsole.debug('no'); | |
// konsole.log('no'); | |
// konsole.info('no'); | |
// konsole.warn('no'); | |
// konsole.error('yes'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment