Skip to content

Instantly share code, notes, and snippets.

@VictorCoding
Forked from tkh44/logWithLabel.js
Created March 1, 2016 22:42
Show Gist options
  • Save VictorCoding/4e4d857076658c5d1fea to your computer and use it in GitHub Desktop.
Save VictorCoding/4e4d857076658c5d1fea to your computer and use it in GitHub Desktop.
Little utility to format logs in a consistent manner
const a = { foo: 'bar' };
const b = 'I like cookies';
export const logWithLabel = (content, label, collapsed = false) => {
if (__DEVELOPMENT__) {
content = Array.isArray(content) ? content : [content];
console[collapsed ? 'groupCollapsed' : 'group'](label.toUpperCase());
content.forEach((c) => {
if (Array.isArray(c) && Object.prototype.toString.call(c[0]) === '[object Object]') {
return console.table(c, Object.keys(c[0]));
}
if (Object.prototype.toString.call(c) === '[object Object]') {
return console.dir(c);
}
let message = c;
if (typeof c === 'string') {
message = `"${c}"`;
}
console.log(message);
});
console.groupEnd();
}
};
//logWithLabel([a, b], 'Received');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment