Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Styling Console Messages
Ayekple Clemence
Ayekple Clemence

Posted on • Originally published atanansewaa.com

     

Styling Console Messages

As developers, we at a point log messages to the console for debugging purposes. Getting unexpected results can be stressful at times especially if you have multiple components in your application. In this case, console messages are extremely helpful.

What if you can style your console messages? Probably not for only debugging purposes but to also warn people to stay away from your work. Interestingly, you can do this by adding the%c specifier to your console statement. Now that you know, open up the Developer Tools and try this in your console.

console.log('%cStyling Console Messages','color:green; background:#33eeaa; font-size:25px;')

Styling Console Messages

%c Specifier

The specifier%c makes it possible for you to add CSS style to the console statement. As shown in the above example,%c applies the CSS style rules in the second parameter to the console statement. Read about other specifiers,Google Developers — Console API Reference.

Other Console Messages

There are other console statements that we can style but let’s focus on applying CSS style to a few of them.

console.log('%cGeneral output of logging information','color: #330000;');console.info('%cInformative logging of information','color: #0080ff;');console.warn('%cOutputs a warning message','color: #ff8000;');console.error('%cOutputs an error message','color: #ff0000;');

Similarly,%c specifier is available for other console statements. Check out the documentation onMDN Web Docs — Console Web APIs.

Applying Multiple Styles

Certainly, multiple CSS styles can be applied to a console statement. Prepend the console statement with%c. Of course, statements after the directive are styled with the CSS styles stated in the parameter.

console.log('Multiple Styles: %cFirst Style. %cSecond Style','color:#085965; font-size:20px','color:#f53c55; font-size:30px')

Notably, there are three parameters specified in the console statement and the style declared in the last two parameters applied to the%c specifier respectively.

Styling Console Messages with an Array of CSS styles

As a matter of fact, CSS styles can get long. However, separating the styles into a variable as an array can be helpful.

conststyles=['color: #4dbb63','background: #999588','font-size: 20px','text-shadow: 2px 2px #615e57','padding: 5px',].join(';');console.log('%cStyling Console Messages',styles);

Note that, thejoin() function was used to convert the array to string.

Conclusion
In conclusion, the console is a powerful tool we can leverage. However, if you are interested in the new features of laravel you can read the article,New in laravel 6.

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
david_j_eddy profile image
David J Eddy
AWS Certified (x4), Automated Testing / Continuous Integration / Delivery / Deployment (CI/CDs), Cloud, Containers, Dev(Sec)Ops, Software Engineer.
  • Email
  • Location
    Internet
  • Education
    BFA from IADT Tampa, 4* AWS Certifications
  • Work
    Infrastructure Engineer
  • Joined

Nice! I did not know it was this easy to add a littlestyle to the Chrome console output. Thank you for this.

CollapseExpand
 
patarapolw profile image
Pacharapol Withayasakpunt
Currently interested in TypeScript, Vue, Kotlin and Python. Looking forward to learning DevOps, though.
  • Location
    Thailand
  • Education
    Yes
  • Joined

You might as well suggest how to style Terminal / Command Prompt using Node.js...

CollapseExpand
 
ayekpleclemence profile image
Ayekple Clemence
I'm a Fullstack Developer and Blogger. I love trying new things and sharing them
  • Location
    Ghana
  • Work
    Fullstack Developer at University of Cape Coast
  • Joined
CollapseExpand
 
nijeesh4all profile image
Nijeesh Joshy
Some one Who Love to Code ,Create and Experiment with Everything
  • Location
    Karur
  • Education
    BE in Mechatornics Engineering
  • Work
    ROR Developer at Mallow technologies pvt.ltd
  • Joined
• Edited on• Edited

There is alsoconsole.table for consoling out tabular data.

developer.mozilla.org/en-US/docs/W...

i just found about this a few days back :) , thought i would share for newbies like me.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm a Fullstack Developer and Blogger. I love trying new things and sharing them
  • Location
    Ghana
  • Work
    Fullstack Developer at University of Cape Coast
  • Joined

More fromAyekple Clemence

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp