Skip to content

Instantly share code, notes, and snippets.

@bvaughn
Last active December 27, 2022 03:13
Show Gist options
  • Star 66 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save bvaughn/810d50d6ade25b784728873daabb905e to your computer and use it in GitHub Desktop.
Save bvaughn/810d50d6ade25b784728873daabb905e to your computer and use it in GitHub Desktop.
const createLogger = (backgroundColor, color) => {
const logger = (message, ...args) => {
if (logger.enabled === false) {
return;
}
console.groupCollapsed(
`%c${message}`,
`background-color: ${backgroundColor}; color: ${color}; padding: 2px 4px;`,
...args
);
console.log(
new Error("stack").stack
.split("\n")
.slice(2)
.join("\n")
);
console.groupEnd();
};
return logger;
};
const log = {
blue: createLogger("#1E88E5", "#90CAF9"),
brown: createLogger("#6D4C41", "#D7CCC8"),
gray: createLogger("#212121", "#BDBDBD"),
green: createLogger("#388E3C", "#A5D6A7"),
red: createLogger("#E53935", "#EF9A9A"),
orange: createLogger("#F4511E", "#FFAB91"),
purple: createLogger("#8E24AA", "#E1BEE7"),
yellow: createLogger("#FFD600", "#FFF59D")
};
module.exports = log;
@ashtonsix
Copy link

Brain, we love you! But we only have 14 hours to save your console! SAVIOUR OF THE UNIVERSE!

@lqs469
Copy link

lqs469 commented Aug 3, 2018

Sometime longerMessage could be Object, will print [Object object]. Maybe use JSON.stringify(longerMessage, null, 2)?

@bvaughn
Copy link
Author

bvaughn commented Sep 25, 2018

Updated the gist to add stack traces as well (within a collapsed console group):

screen shot 2018-09-25 at 8 17 07 am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment