DEV Community

Omar Dulaimi
Omar Dulaimi

Posted on • Updated on

Pure Nodejs Logger

Did you know that the 𝗡𝗼𝗱𝗲𝗷𝘀 console module provides a 𝗖𝗼𝗻𝘀𝗼𝗹𝗲 class, unlike the browser?

This is how the default global console object is defined under the hood:
𝗻𝗲𝘄 𝗖𝗼𝗻𝘀𝗼𝗹𝗲({ 𝘀𝘁𝗱𝗼𝘂𝘁: 𝗽𝗿𝗼𝗰𝗲𝘀𝘀.𝘀𝘁𝗱𝗼𝘂𝘁, 𝘀𝘁𝗱𝗲𝗿𝗿: 𝗽𝗿𝗼𝗰𝗲𝘀𝘀.𝘀𝘁𝗱𝗲𝗿𝗿 });

What most people don't know is that you can configure the console to write to files instead of the standard output and error streams.

This allows you create pure and configurable 𝗡𝗼𝗱𝗲𝗷𝘀 loggers; zero dependencies required.

The constructor options include:

𝘀𝘁𝗱𝗼𝘂𝘁: Writable output stream.
𝘀𝘁𝗱𝗲𝗿𝗿: Writable error stream
𝗶𝗴𝗻𝗼𝗿𝗲𝗘𝗿𝗿𝗼𝗿𝘀: Enabled by default
𝗰𝗼𝗹𝗼𝗿𝗠𝗼𝗱𝗲: Default is auto
𝗶𝗻𝘀𝗽𝗲𝗰𝘁𝗢𝗽𝘁𝗶𝗼𝗻𝘀: Passed to 𝘂𝘁𝗶𝗹.𝗶𝗻𝘀𝗽𝗲𝗰𝘁()
𝗴𝗿𝗼𝘂𝗽𝗜𝗻𝗱𝗲𝗻𝘁𝗮𝘁𝗶𝗼𝗻: Default is 2

Check the snap below to see how it can be used.

Did you learn something new today?

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

pure nodejs logger

Top comments (0)