DEV Community

Omar Dulaimi
Omar Dulaimi

Posted on • Edited on

Different ways to log messages in Nodejs

How many ways do you know ?
According to my little research, there are 3 different ways you can log messages in π—‘π—Όπ—±π—²π—·π˜€.

1- π—°π—Όπ—»π˜€π—Όπ—Ήπ—².𝗹𝗼𝗴: We all know this one and have used it extensively in Javascript. What people don't know that under the hood it actually uses π—½π—Ώπ—Όπ—°π—²π˜€π˜€.π˜€π˜π—±π—Όπ˜‚π˜.π˜„π—Ώπ—Άπ˜π—² with a line break.

2- π—½π—Ώπ—Όπ—°π—²π˜€π˜€.π˜€π˜π—±π—Όπ˜‚π˜.π˜„π—Ώπ—Άπ˜π—²: Unlike the previous one, this outputs messages on the same line; so you're going to need to manually add like breaks.

3- π—³π˜€.π˜„π—Ώπ—Άπ˜π—²π—¦π˜†π—»π—°: Also requires manual line breaks. Writes to π—½π—Ώπ—Όπ—°π—²π˜€π˜€.π˜€π˜π—±π—Όπ˜‚π˜.π˜„π—Ώπ—Άπ˜π—² like π—°π—Όπ—»π˜€π—Όπ—Ήπ—².𝗹𝗼𝗴. This method though is a little unique. It takes an integer as the first argument, something called a 𝗳𝗢𝗹𝗲 π—±π—²π—°π—Ώπ˜†π—½π˜π—Όπ—Ώ. To make it write to the π˜€π˜π—±π—Όπ˜‚π˜, you have to set it to 1. I'll talk about it in my next post.

Did you learn something new today?

Like and share this post, and follow me for more!

logging messages in nodejs

Top comments (0)