Add Styles to Console Statements

By  on  

I was recently checking out Google Plus because they implement some awesome effects.  I opened the console and same the following message:

WARNING!

Using this console may allow attackers to impersonate you and steal your information using an attack called Self-XSS.

Do not enter or paste code that you do not understand.

I wasn't surprised to see that message but what I did notice was that the text was red and the background was yellow.  The text was even a bit bigger.  How did they do it?  Pretty easily:

console.log("%c%s",
            "color: red; background: yellow; font-size: 24px;",
            "WARNING!");

The first argument is the order of style and message, the second is the style set, and the last is the desired message.

As to why you'd want to use this?  If it helps you identify debug information easier in the console, you may consider calling more attention to some messages!

Recent Features

  • By
    I’m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

Incredible Demos

  • By
    Fancy FAQs with jQuery Sliders

    Frequently asked questions can be super boring, right? They don't have to be! I've already shown you how to create fancy FAQs with MooTools -- here's how to create the same effect using jQuery. The HTML Simply a series of H3s and DIVs wrapper...

  • By
    Adding Events to Adding Events in MooTools

    Note: This post has been updated. One of my huge web peeves is when an element has click events attached to it but the element doesn't sport the "pointer" cursor. I mean how the hell is the user supposed to know they can/should click on...

Discussion

  1. Easy, if one controls the browser and therefor the console, that has to display this ;) But Firefox supports this as well, and only IE prints both, the format string as the string itself 1:1 into F12.

  2. There’s a JS library that makes styling comments much easier:

    https://github.com/astoilkov/console.message

  3. By replacing your formatting placeholders with %c%o, it will output a readable repr for any type of object you pass in, not just the str value.

    https://jsfiddle.net/jsatt/Lta6p2jz/2/

  4. very cool advice! my console is very colour ;)

  5. With some trickery you can inject images too!

    console.log("%cDuckie.TV", "color:transparent; font-size: 16pt; line-height: 125px; padding:25px; padding-top:30px; padding-bottom:60px; background-image:url(http://duckietv.github.io/DuckieTV/img/icon128.png); background-repeat:no-repeat; ", "quack!\n\n\n\n\n\n");
    
  6. Amelia BR

    I find it amusing that this post starts with an example of a warning not to copy & paste code into the console, and ends with me wanting to copy & paste code from people’s comments into the console.

    But it’s okay, cause I sort of understand it, right?

  7. schadeck

    I like to put an object at the top of my script when I am developing to create a shortcut for styling my logs:

    // for styling console.log
    // EXAMPLE: console.log(sty.sty, sty.red, 'thingToLog:' ,+ foo);
    var sty = {
        sty: '%c%s',
        grn: 'background:#9c9;color:#141;padding:.25em;line-height:1.5em;',
        ylw: 'background:#ff0;color:#993;padding:.25em;line-height:1.5em;',
        red: 'background:#c99;color:#411;padding:.25em;line-height:1.5em;',
        org: 'background:#da0;color:#830;padding:.25em;line-height:1.5em;',
        gry: 'background:#666;color:#fff;padding:.25em;line-height:1.5em;',
        prp: 'background:#97e;color:#405;padding:.25em;line-height:1.5em;',
        blu: 'background:#8ac;color:#036;padding:.25em;line-height:1.5em;'
    };
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!