Skip to content

Instantly share code, notes, and snippets.

@jchip
Created March 14, 2017 03:12
Show Gist options
  • Save jchip/5a4ef6efd20fbb6b011c39b4e17af763 to your computer and use it in GitHub Desktop.
Save jchip/5a4ef6efd20fbb6b011c39b4e17af763 to your computer and use it in GitHub Desktop.
makeSimpleLogger
function makeSimpleLogger() {
const levels = ["verbose", "info", "warn", "debug"];
const tags = levels.reduce((a, l) => (a[l] = l.toUpperCase(), a), {});
function log(tag, args) {
args = Array.prototype.concat.apply([`${tags[tag]}:`], args);
if (console[tag]) {
console[tag].apply(console, args);
} else {
console.log.apply(console, args);
}
}
return levels.reduce((a, l) => {
a[l] = function () { log(l, arguments) };
return a;
}, {});
}
const logger = makeSimpleLogger();
logger.info("hello");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment