top | item 5540603

Console.log with style

236 points| adamschwartz | 13 years ago |adamschwartz.co | reply

56 comments

order
[+] goodside|13 years ago|reply
The problem with wrapping console.log is that you lose the ability to know what line number initiated the log entry. The line number Chrome gives for the log line will always be where you call console.log inside the wrapper, not the line where you call the wrapper. Since there's no way to grab the line number of a caller in JS (Chrome's dev console uses browser hooks unavailable in JS), you can't even emulate the line number reporting yourself as a suffix to the log entry.
[+] gjuggler|13 years ago|reply
Using straight console.log statements seems to be non-negotiable for effective debugging, at least until a good workaround for the above is found.

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.

[+] jQueryIsAwesome|13 years ago|reply
I found a way... well, sort of:

    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")()
[+] dribnet|13 years ago|reply
> there's no way to grab the line number of a caller in JS

But this is easily fixed by converting the function call to a macro, right?

Oh wait - this isn't a ClojureScript library... so nevermind.

[+] hpaavola|13 years ago|reply
It's missing "This site is best viewed in IE5"... Err, I mean Chrome, but it's the same.
[+] coldtea|13 years ago|reply
It's not supposed to be used everywhere.

It's a developer tool for browsers that have the requisite developer tools support.

[+] jQueryIsAwesome|13 years ago|reply
This "console"... IE5 didn't even have console... and event the current one IE haves is a joke compared to firefox or chrome.
[+] baddox|13 years ago|reply
I'm on OS X with an external keyboard, and I gave up after trying 3 or 4 keyboard combinations. I literally don't know for sure what key any of the three symbols is referring to.
[+] ddadams10|13 years ago|reply
This is command + option + J

If you're using a windows keyboard, this should be the windows key + alt + J

[+] D9u|13 years ago|reply
On FreeBSD 9x it was Ctrl + Shift + i, so I inferred that what I initially thought was a lower case "L" was actually an upper case "I." (the Shift)
[+] suyash|13 years ago|reply
Try this: console.log('%cHello world', 'font-size:100px;color:#fff;text-shadow:0 1px 0 #ccc,0 2px 0 #c9c9c9,0 3px 0 #bbb,0 4px 0 #b9b9b9,0 5px 0 #aaa,0 6px 1px rgba(0,0,0,.1),0 0 5px rgba(0,0,0,.1),0 1px 3px rgba(0,0,0,.3),0 3px 5px rgba(0,0,0,.2),0 5px 10px rgba(0,0,0,.25),0 10px 10px rgba(0,0,0,.2),0 20px 20px rgba(0,0,0,.15);');
[+] suyash|13 years ago|reply
btw orignal Creator is Sindre Sorhus -- try this var _log = console.log; console.log = function() { _log.call(console, '%c' + [].slice.call(arguments).join(' '), 'color:transparent;text-shadow:0 0 2px rgba(0,0,0,.5);'); };

Then run some console.log("Hello Console") or other statements after executing the function above and see the prank ;)

[+] jastanton|13 years ago|reply
These are the CSS attr's they accept

  ["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 blocks
[+] Aardwolf|13 years ago|reply
Um, great, how am I supposed to trust the console if even logging in that one can be screwed up?
[+] tiwazz|13 years ago|reply
Really cool!

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?

[+] Skoofoo|13 years ago|reply
I once tried to write a markup parser with slashes for italics, then I understood why no parser seems to use them. It interferes with URLs, dates, slashes as "or", etc.
[+] solox3|13 years ago|reply
Cool find aside, it must degrade gracefully on a non-Chrome console.

Edit: also, try log('this is code: `a =* `= b* == c`');

[+] publicfig|13 years ago|reply
Is there anything I need to get this to work? It doesn't work for me in OS X on Chrome 23
[+] afschwartz|13 years ago|reply
Update to Chrome 26 (latest stable)
[+] moron4hire|13 years ago|reply
This seems like a terrible idea. Another potential failure point for no good reason.
[+] xer0x|13 years ago|reply
Holy crap that's beautiful. Thanks for showing this off.
[+] remzisenel|13 years ago|reply
firefox 20.0 on mac osx 10.8.2, not working for me :(