Skip to content

Instantly share code, notes, and snippets.

@omichelsen
Last active March 5, 2016 21:10
Show Gist options
  • Save omichelsen/bb27a2991bfd4b741764 to your computer and use it in GitHub Desktop.
Save omichelsen/bb27a2991bfd4b741764 to your computer and use it in GitHub Desktop.
JS: Logger
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