Last active
October 31, 2018 20:10
-
-
Save js2me/0cbcece8eab2e8c78b3ddee8c28cc561 to your computer and use it in GitHub Desktop.
Better object log using console in web
This file contains hidden or 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
console.logObject = function(...args) { | |
const log = args.reduce((logs, object, index) => { | |
if (typeof object === 'object' && !(object instanceof Array)) { | |
logs.push(`\r\n{ `) | |
logs.push.apply( | |
logs, | |
Object.keys(object).reduce((values, key) => { | |
values.push( | |
`\r\n ${key}:`, | |
typeof object[key] === 'function' ? '[function]' : object[key], | |
', ' | |
) | |
return values | |
}, []) | |
) | |
logs.push('\r\n}') | |
} else logs.push('\r\n', object) | |
return logs | |
}, []) | |
this.group.call(this, ...log) //eslint-disable-line | |
this.groupCollapsed('call stack ->') | |
this.log.call(this, ...Error().stack.split(/\n/).slice(2).map(log => log + '\n')) //eslint-disable-line | |
this.groupEnd() | |
this.groupEnd() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment