Created
February 19, 2021 16:57
-
-
Save farskid/b6cbe78d207c44f947a887f5196b45af to your computer and use it in GitHub Desktop.
Programmatically assign log levels to chalk colorful methods
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
const chalk = require("chalk"); | |
const logLevelToChalkColors = { | |
error: chalk.red, | |
log: chalk.white, | |
info: chalk.blueBright, | |
}; | |
// Programmatically generate log methods based on the level to color config above ^ | |
const logger = Object.entries(logLevelToChalkColors).reduce( | |
(logger, [level, colorMethod]) => { | |
return { | |
...logger, | |
[level](...args) { | |
console.log(colorMethod(...args)); | |
}, | |
}; | |
}, | |
{} | |
); | |
module.exports = logger; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment