Created
October 10, 2019 18:20
-
-
Save johndstein/367ab65a8c8f538727d0368ce81c617b to your computer and use it in GitHub Desktop.
super simple json log for fe
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
#!/usr/bin/env node | |
'use strict'; | |
class Log { | |
constructor(options) { | |
options = options || {}; | |
this.level = 'off'; | |
this.levels = { | |
debug: 0, | |
info: 1, | |
warn: 2, | |
error: 3, | |
off: 4 | |
}; | |
Object.assign(this, options); | |
for (const level of Object.keys(this.levels)) { | |
this[level] = (o, spaces = this.spaces) => { | |
if (this.levels[level] >= this.levels[this.level]) { | |
const sf = JSON.stringify(o, null, spaces); | |
if (sf.startsWith('{')) { | |
console.log(sf); | |
} | |
else { | |
o = { message: o }; | |
console.log(JSON.stringify(o, null, spaces)); | |
} | |
} | |
}; | |
} | |
} | |
} | |
const log = new Log({ level: 'info', spaces: 7 }); | |
log.debug({ msg: 'debug!' }); | |
log.info(1, 3); | |
log.warn({ msg: 'warn!' }); | |
log.error({ msg: 'error!' }); | |
const log2 = new Log({ level: 'error' }); | |
log2.debug({ msg: 'debug!' }); | |
log2.info(1, 3); | |
log2.warn({ msg: 'warn!' }); | |
log2.error({ msg: 'error!' }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment