Skip to content

Instantly share code, notes, and snippets.

@omichelsen omichelsen/logger.js
Last active Mar 5, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.