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)