DEV Community

NodeJS: How To Colorize Text

miku86 on August 28, 2019

Intro So we installed NodeJS on our machine. We also know How to Get External Packages. Now we want to learn how to colorize text usin...
Collapse
 
avasconcelos114 profile image
Andre Vasconcelos

I recently used the same thing to have color-coded logging on my node app :)

looks a little something like this:

function debug(message) {
    const timestamp = new Date().toString()
    console.log('\x1b[36m%s\x1b[0m', `${timestamp} - ${message}`)
}

function error(message) {
    const timestamp = new Date().toString()
    console.error('\x1b[31m%s\x1b[0m', `${timestamp} - ${message}`)
}
Collapse
 
miku86 profile image
miku86

Hey v 1 r t l & Andre,

thanks for your tips,
I will have a look at it.

If anyone is interested in further information:
various escape codes
bigger discussion on stackoverflow