r/javascript • u/ya3ya6 • May 19 '19
Did you know you can style console.log text?
I just found it today.
With append "%c" at first of log string, you can pass style as second parameter.
Example: console.log("%c0_0","font-size: 50px;border: 5px solid #aef; border-radius: 50%;padding: 30px;color: #359")
More examples : https://stackoverflow.com/questions/7505623/colors-in-javascript-console
10
u/ya3ya6 May 19 '19
Wow, animated images(gifs) will also work, try :
console.log("%c.","padding:100px;background-size:cover;background-image:url('http://clipart-library.com/images/6Tp6Mna8c.gif')")
2
u/catwhatcat May 19 '19
console.log("%c.","padding:50px;background-size:cover;background-image:url('https://media3.giphy.com/media/5kq0GCjHA8Rwc/giphy.gif')")
2
u/ya3ya6 May 19 '19
console.log("%c.","padding:30px 510px;background-size:contain;background-repeat-y:no-repeat;background-image:url('https://media.tenor.com/images/59478acf4603b91da87453051869fa80/tenor.gif');");
console.log("%c.","padding:30px 450px;background-size:contain;background-image:url('https://media3.giphy.com/media/5kq0GCjHA8Rwc/giphy.gif');background-repeat-y:no-repeat;margin-left: 60px;");
console.log("%c.","padding:30px 510px;background-size:contain;background-repeat-y:no-repeat;background-image:url('https://media.tenor.com/images/59478acf4603b91da87453051869fa80/tenor.gif');");
19
u/davidkonrad May 19 '19
Yeah, and that is one of those "cool" completely unnecessary features with poor support, you in the end realise just causes problems.
For example, in a browser-module project (not made public yet) I used some styling in the console output to make certain kind of errors more clear. The test however is running mocha / chai in node, and instantiate the browser-module, and must pass the console object in order to make the browser-module work "serverside". Then %c becomes less attractive, because nodes' console not support this feature === %c0_0","font-size: 50px;border: 5px solid #aef; border-radius: 50%;padding: 30px;color: #359 (as example) is printed out as text in the console. Not really what you want users to see, when they evaluate your project :)
5
u/Hanlonsrazorburns May 19 '19
Well generally speaking you turn off your console logs BEFORE you deploy to production. This is very useful when you have built in debugging logs and need to separate out the information quickly.
1
u/davidkonrad May 20 '19
Yes, totally agree, if you have som kind of consoling you are using as helper for yourself / team when you are in development.
But I was merely talking about that kind of info you really need / want to pass to the end user. I actually styled
console.log()because a certain kind of "error" (in production) is an error (or misconfiguration, wrong use, bad use) but not severe or crashing anything. Just a hint that "x is wrong because of y and is being skipped". A category between a real error and "useful info". And I needed also to create tests for those scenarios, and then I realised that nodes console not support (that kind of) styling. And that the browsers styling capabilities are rather new (is it possible in FF yet, without a Firebug plugin?) and so on.
1
u/ya3ya6 May 19 '19 edited May 19 '19
console.log("%c*_0","font-size: 30px;border: 5px solid #aef; border-radius: 50%;padding: 10px;color: #359"); console.log('%cYou seems suspicious, please dont consider hacking or copy this website, cuase: i p== *_*','padding:10px;color:red;background-color:#aef'); console.log("%cThat's a gun buy the way, and that means:",'color:red;background-color:#aef;padding:10px;'); console.log("%cI'll kill you",'color:red;background-color:red;padding:10px;');
1
3
u/elephantengineer May 19 '19
Discovered this a while back from browsing facebook with the console open. Decent use case for it as well.
1
u/-Pad May 19 '19
What would be a useable case for this be though?
3
u/ya3ya6 May 19 '19
aside from custom styling logs, and make new log systems(like vividlog), it's attractive for developers, so some use cases are :
https://airdev.co/ : it's created by bubble (a website creator), so bubble promote itself to developers.
bbc.com : bbc promote it's recruitment to developers.
2
2
u/examinedliving May 19 '19
I think it’s only useful if you are developing a testing platform for an enterprise app. At the level of individual and small biz, it takes too much energy for too little reward. I tried to write a shortcut JS class once, but the privacy of Console methods made it too much of a pain. Might be different now though.
1
119
u/Earhacker May 19 '19
My work (and many others) use this to put a recruitment link in the console. It gets zero click-throughs but whatever, it passed the time one Friday afternoon months ago.
The BBC do this too. So do Reddit but it's not styled and is obscured by an ass-ton of console errors.