Last active
July 26, 2018 15:17
-
-
Save OutThisLife/7f19fe31cb8e29332426c5ba25e7efcf to your computer and use it in GitHub Desktop.
Simple template-literal colorized logger --- log.info`The only thing that will highlight is ${thisVariableHere}`
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
/** | |
* Example usage: | |
log.warn`Some message with ${variables} highlighted`; | |
log.blue`Some message everything highlighted`; | |
log.info`Some message with ${variables} highlighted`; | |
log.blueBG`Some message everything highlighted`; | |
*/ | |
const colours = { | |
reset: '\x1b[0m', | |
fx: { | |
bright: '\x1b[1m', | |
dim: '\x1b[2m', | |
underscore: '\x1b[4m', | |
blink: '\x1b[5m', | |
reverse: '\x1b[7m', | |
hidden: '\x1b[8m' | |
}, | |
font: { | |
black: '\x1b[30m', | |
red: '\x1b[31m', | |
green: '\x1b[32m', | |
yellow: '\x1b[33m', | |
blue: '\x1b[34m', | |
magenta: '\x1b[35m', | |
cyan: '\x1b[36m', | |
white: '\x1b[37m' | |
}, | |
bg: { | |
black: '\x1b[40m', | |
red: '\x1b[41m', | |
green: '\x1b[42m', | |
yellow: '\x1b[43m', | |
blue: '\x1b[44m', | |
magenta: '\x1b[45m', | |
cyan: '\x1b[46m', | |
white: '\x1b[47m' | |
} | |
}; | |
const loggy = { | |
theme: { | |
info: 'cyan', | |
warn: 'red', | |
success: 'green' | |
}, | |
_raw(...args) { | |
console.log( | |
`${this.colour}${[] | |
.concat(...args) | |
.filter(a => a) | |
.join('')}${colours.reset}` | |
); | |
}, | |
_keys(tag = '', strings, ...keys) { | |
console.log( | |
`${colours.bg[this.colourKey]}[${tag}]:${colours.reset}`, | |
strings.reduce((acc, id, i) => { | |
acc += id; | |
if (keys[i]) { | |
acc += `${this.colour}${keys[i]}`; | |
} | |
acc += colours.reset; | |
return acc; | |
}, '') | |
); | |
}, | |
set colour(v) { | |
const group = /bg/.test(v.toLowerCase()) ? 'bg' : 'font'; | |
const key = v.replace(/bg/i, ''); | |
const value = colours[group][key]; | |
this._colour = { key, value }; | |
}, | |
get colour() { | |
return this._colour.value; | |
}, | |
get colourKey() { | |
return this._colour.key; | |
} | |
}; | |
module.exports = new Proxy(loggy, { | |
get(target, key) { | |
if (key in target) { | |
return target[key]; | |
} else if (Object.keys(colours.font).includes(key.replace(/bg/i, ''))) { | |
target.colour = key; | |
return (...args) => target._raw(...args); | |
} else if (key in target.theme) { | |
target.colour = target.theme[key]; | |
return (...args) => target._keys(key, ...args); | |
} | |
return target; | |
} | |
}); | |
/** | |
* Example usage: | |
log.warn`Some message with ${variables} highlighted`; | |
log.blue`Some message everything highlighted`; | |
log.info`Some message with ${variables} highlighted`; | |
log.blueBG`Some message everything highlighted`; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment