Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create formatted javascript console log messages

I 'waddled' by the Console in Chrome on Facebook today.
Surprisingly I got this message in the console.

Now my question is:
How is this possible?
I know that there are a few 'exploit' methods for the console, but how can you make such font formatting in the console? (and is it console.log?)

like image 609
Anders Kjeldsen Avatar asked Mar 03 '14 19:03

Anders Kjeldsen


People also ask

How do you log messages in JavaScript?

log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console. log(A);


2 Answers

Yes, you can format the console.log() with something like this:

console.log("%cExtra Large Yellow Text with Red Background", "background: red; color: yellow; font-size: x-large"); 

Note the %c preceding the text in the first argument and the style specifications in the second argument. The text will look like your example.

See Google's "Styling Console Output with CSS" or FireBug's Console Documentation for more details.

The documentation links also include some other neat tricks like including object links in a console log as well.

like image 119
Dallas Avatar answered Sep 24 '22 13:09

Dallas


Try this:

console.log("%cThis will be formatted with blue text", "color: blue"); 

Quoting the docs,

You use the %c format specifier to apply custom CSS rules to any string you write to the Console with console.log() or related methods.

Source: https://developers.google.com/web/tools/chrome-devtools/console/console-write#styling_console_output_with_css

like image 33
blurfus Avatar answered Sep 22 '22 13:09

blurfus