Skip to content

Instantly share code, notes, and snippets.

@shivam-tripathi
Created March 16, 2018 10:55
Show Gist options
  • Save shivam-tripathi/e7fc649c1c1d7d07601e3038932539e8 to your computer and use it in GitHub Desktop.
Save shivam-tripathi/e7fc649c1c1d7d07601e3038932539e8 to your computer and use it in GitHub Desktop.
/*
* 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