Skip to content

Instantly share code, notes, and snippets.

@m1m1s1ku
Last active July 25, 2017 01:41
Show Gist options
  • Save m1m1s1ku/a511781f86d9606a31e004e6ca2ec281 to your computer and use it in GitHub Desktop.
Save m1m1s1ku/a511781f86d9606a31e004e6ca2ec281 to your computer and use it in GitHub Desktop.
Debug beautifully (log function pure js)
/**
* log(anything);
* Shorter
*/
log = function(){
return Function.prototype.bind.call(console.log, console, "🛠️");
}();
/**
* Debug beautifully
* @param {any} params
* @param {string} type : 'log', 'info', 'warn', 'error'
* @param {string} emoji : Emoji to use (or another string)
* @param {boolean} breakHere : Add breakpoint for debugger
* @param {boolean} callStack : if call stack should be logged
*/
var log = (obj, type = 'log', emoji = '🛠️', breakHere = false, callStack = false) => {
var debug = null;
if (typeof obj.reduce === 'function')
debug = obj.reduce(function (result, item) {
var key = Object.keys(item)[0];
result[key] = item[key];
return result;
});
else
debug = obj;
console[type](emoji, debug);
if (breakHere)
debugger;
if (callStack)
if (typeof console.trace === 'function')
console.trace();
else
console.log(new Error('trace').stack);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment