Last active
September 13, 2022 02:34
-
-
Save tkh44/ef96c631620de1a76854 to your computer and use it in GitHub Desktop.
Little utility to format logs in a consistent manner
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 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