Skip to content

Instantly share code, notes, and snippets.

@farskid
Created February 19, 2021 16:57
Show Gist options
  • Save farskid/b6cbe78d207c44f947a887f5196b45af to your computer and use it in GitHub Desktop.
Save farskid/b6cbe78d207c44f947a887f5196b45af to your computer and use it in GitHub Desktop.
Programmatically assign log levels to chalk colorful methods
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