Last active
March 5, 2016 21:10
-
-
Save omichelsen/bb27a2991bfd4b741764 to your computer and use it in GitHub Desktop.
JS: Logger
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
export const LogLevels = { | |
log : 0, | |
info : 1, | |
warn : 2, | |
error : 3 | |
} | |
function clone(arr) { | |
return arr.map(a => JSON.parse(JSON.stringify(a))) | |
} | |
function log(type, ...args) { | |
if (LogLevels[type] < this.logLevel) return | |
this.history.push([Date.now(), clone(args)]) | |
this.targets.forEach(target => target[type](...args)) | |
} | |
export default class Logger { | |
constructor(logLevel = LogLevels.log, ...targets) { | |
this.history = [] | |
this.logLevel = logLevel | |
this.targets = targets.length ? targets : [console] | |
Object.keys(LogLevels).forEach(key => { | |
this[key] = log.bind(this, key) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment