Skip to content

Instantly share code, notes, and snippets.

@ruevaughn
Last active September 11, 2021 04:24
Show Gist options
  • Save ruevaughn/a5f32fc7e7c6c84033ed2663fb0308c5 to your computer and use it in GitHub Desktop.
Save ruevaughn/a5f32fc7e7c6c84033ed2663fb0308c5 to your computer and use it in GitHub Desktop.
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