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 @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

  • 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...

Incredible Demos

  • By
    Display Images as Grayscale with CSS Filters

    CSS filters aren't yet widely supported but they are indeed impressive and a modern need for web imagery.  CSS filters allow you to modify the display of images in a variety of ways, one of those ways being displaying images as grayscale. Doing so requires the...

  • By
    jQuery Comment Preview

    I released a MooTools comment preview script yesterday and got numerous requests for a jQuery version. Ask and you shall receive! I'll use the exact same CSS and HTML as yesterday. The XHTML The CSS The jQuery JavaScript On the keypress and blur events, we validate and...

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!