Created
March 16, 2018 10:55
-
-
Save shivam-tripathi/e7fc649c1c1d7d07601e3038932539e8 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
/* | |
* Logger class | |
* - debug | |
* - info | |
* - error | |
* - warn | |
* - setLevel | |
*/ | |
export default class Log { | |
constructor(level = 'INFO') { | |
this.logLevel = level; | |
} | |
log(args) { | |
args = Array.prototype.slice.call(args); | |
args.unshift(`[${this.logLevel}] `); | |
let console = window.console; | |
if (console && console.log && console.log.apply) { | |
try { | |
console.log.apply(this, args); | |
} catch(e) {}; | |
} | |
} | |
distinctLog(level, args) { | |
let prevLevel = this.logLevel; | |
this.logLevel = level; | |
this.log(args); | |
this.logLevel = prevLevel; | |
} | |
debug(args) { | |
if (this.logLevel === 'DEBUG') { | |
this.log(args); | |
} | |
} | |
info(args) { | |
if (this.logLevel === 'INFO') { | |
this.log("INFO", arguments); | |
} | |
} | |
error(args) { | |
this.distinctLog('ERROR', args); | |
} | |
warn(args) { | |
this.distinctLog('WARN', args); | |
} | |
setLevel(level) { | |
this.logLevel = level; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment