simple personalized logging

some examples how to log data to the console

by juve

HTML

<!--
You need to open the console window (F12) to see the effect of the code below. Just press "Run" when the console is open.
-->

JavaScript

console.log("== Log and access later Problem ==")
a = {v:[1,2,3]}; console.log(a)   // ╔═ Problem ══════════════════════════╗
// Object {v: Array[3]}           // ║ When opening the object after the  ║
a.v.splice(0); console.log(a)     // ║  a.v.splice call, v will be empty. ║
// Object {v: Array[0]}           // ╚════════════════════════════════════╝

// ╔═ Solution ══════════════════╗
// ║ Build your own log function ║
// ║ to serialize the objects    ║
// ╚═════════════════════════════╝
warn = function(t){ console.warn( JSON.stringify(t) ) }
warn("Stringify test")
a = {v:[1,2,3]}; warn(a)  // ╔═ Advantage/Drawback ══════════════╗
// {"v":[1,2,3]}          // ║ All data instantly visible.       ║
a.v.splice(0); warn(a)    // ║ Might cause trouble with big data ║
// {"v":[]}               // ╚═══════════════════════════════════╝

// Design your personal log functions that takes 1..n parameters and applies them to console log
warnObj = function() { console.warn.apply(console, (arguments.length >= 1)? [].slice.call(arguments, 0) : []) };
warnStr = function() { console.warn(JSON.stringify((arguments.length >= 1)? [].slice.call(arguments, 0) : [])) };
w = warnObj; w("== Normal warn ==");  w(a={x:[1]});  a.x.splice(0);  w(a)
w = warnStr; w("== String warn ==");  w(a={x:[1]});  a.x.splice(0);  w(a)