Console.log with style
adamschwartz.co
adamschwartz.co
var __LINE__ = (new Error).stack.split("\n")[1].match(/:([0-9]+):/)[1];Unless you’re running it on an old IE that doesn’t host the `window.console` object.
Ideally, you’ll want those calls stripped at some point, in your build script.
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();But this is easily fixed by converting the function call to a macro, right?
Oh wait - this isn't a ClojureScript library... so nevermind.
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")()Here is a demo: http://jsfiddle.net/bgrins/MZWtG/.
[1] http://www.softwareishard.com/blog/firebug/firebug-tip-style...
It's a developer tool for browsers that have the requisite developer tools support.
Basically just open the javascript console.
If you're using a windows keyboard, this should be the windows key + alt + J
Then run some console.log("Hello Console") or other statements after executing the function above and see the prank ;)
["background", "border", "color", "font", "line", "margin", "padding", "text", "-webkit-background", "-webkit-border", "-webkit-font", "-webkit-margin", "-webkit-padding", "-webkit-text"]
Try this: console.log("%c ", 'background: url("http://placekitten.com/200/200"); padding: 5000px')
edit:
put my code snippets in code blockshttp://stackoverflow.com/questions/3326650/console-is-undefi...
I don't want to sound nitpicky buuuuuut in text based emails folks have been using bold, /italics/, _underlining_, and {{{ preformatted }}} text for a long time with slightly different syntax. How attached are you to the one you chose? Would you be open to accepting a patch to use the older syntax?
Submit a PR here https://github.com/adamschwartz/log/pulls and we'll consider it if there is wide support.
Edit: also, try log('this is code: `a =* `= b* == c`');
https://developers.google.com/chrome-developer-tools/docs/co...