Colorful Node.js Message Logging with Chalk

By  on  

As you work more and more with Node.js, you start to see the value of good logging, especially to the console.  The problem you run into, however, is that constantly adding logged messages means that the most important messages can get lost in the shuffle.  Info messages should look one way and app-killing errors should look another.  The Node.js module to help us accomplish custom formatting of messages?  Chalk!

Chalk has a very easy to follow, simple to use API.  Here are a few code examples:

const chalk = require('chalk');

// style a string
chalk.blue('Hello world!');

// combine styled and normal strings
chalk.blue('Hello') + 'World' + chalk.red('!');

// compose multiple styles using the chainable API
chalk.blue.bgRed.bold('Hello world!');

// pass in multiple arguments
chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz');

// nest styles
chalk.red('Hello', chalk.underline.bgBlue('world') + '!');

You can chain methods like bold onto color names, and visa versa.  You can also append Chalk'd strings or add them as separate arguments.  Chalk is very flexible without modifying the String prototype which is impressive.

Apparently over 5,000 projects use Chalk and I can see why!  Big problems should come with big colors and lessor debugging information should be less prominent.  Happy coding!

Recent Features

  • By
    CSS Animations Between Media Queries

    CSS animations are right up there with sliced bread. CSS animations are efficient because they can be hardware accelerated, they require no JavaScript overhead, and they are composed of very little CSS code. Quite often we add CSS transforms to elements via CSS during...

  • By
    6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

Incredible Demos

  • By
    Scrolling “Agree to Terms” Component with MooTools ScrollSpy

    Remember the good old days of Windows applications forcing you to scroll down to the bottom of the "terms and conditions" pane, theoretically in an effort ensure that you actually read them? You're saying "No David, don't do it." Too late -- I've done...

  • By
    Web Notifications API

    Every UI framework has the same set of widgets which have become almost essential to modern sites: modals, tooltips, button varieties, and notifications.  One problem I find is each site having their own widget colors, styles, and more -- users don't get a consistent experience.  Apparently the...

Discussion

  1. Oh my. THANK YOU! This is the exact chalk effect I was looking for one of my projects.

  2. Looks like a cool tool – and pretty colour scheme too :D

  3. Randy

    Yeah idk I’d rather not need to pick the color myself for a logged error message. If you use better-console or captains-log you can just use log.error() or log.info() and get coloring which makes sense.

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