by goodside · 13y ago · 0 comments
Here is a demo: http://jsfiddle.net/bgrins/MZWtG/.
For using Chrome's logging styles, we define a few short helper functions — l1(), l2(), l3(), etc. — that return predefined CSS strings, then doing our logging like
console.log("%cThis is a heading", l1())
console.log("%cLess important stuff", l2())
This means you're only adding 6 or so characters to get nice styled console messages that also maintain the nice line numbers and links to the source. Plus, it's similar enough to h1, h2, etc. that it's easy to remember.We found that varying the font color (black vs. gray) and margin-left (2em, 4em) were most helpful in differentiating more and less important log messages.
console.assert(str.length === 10, "String is not 10 characters long!");
and console.group/groupEnd console.group("sums");
console.log(1 + 1);
console.log(2 - 3);
console.groupEnd(); var __LINE__ = (new Error).stack.split("\n")[1].match(/:([0-9]+):/)[1];Ideally, you’ll want those calls stripped at some point, in your build script.
var log = function(){
args = [].slice.call(arguments);
args.unshift(console);
return console.log.bind.apply(console.log, args)
}
But you have to add extra parenthesis at the end, hopefully is not too much overhead for most devs. log("message")()But this is easily fixed by converting the function call to a macro, right?
Oh wait - this isn't a ClojureScript library... so nevermind.