Last active
September 11, 2021 04:24
-
-
Save ruevaughn/a5f32fc7e7c6c84033ed2663fb0308c5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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