Console.log Alternatives in Production

Console.log Alternatives in Production

So I saw the tweet above the other day and it caught my attention, what are the alternatives? Why should I use them? Why is it better than console.log?

As developers at the early stage of learning javascript, console.log was one of the first set of things we learned. While using NodeJs REPL console.log was a quick way to debug. It required no dependencies since it is based on the NodeJs console module and is easy to use

Today, logging can be done for various reasons, like for debugging, error tracking, performance tracking, system usage analyses, and many more.

So it begs the question of why does console.log need alternatives.

Why console.log alternatives?

Well, we can say because a developer felt like creating a new NPM package but there are other tangible reasons.

  • console.log() could not be toggled on/off, it was always on, but also it could be done manually by overwriting the console.log method, the snippet below shows how it is done in production.
if( process.env.NODE_ENV == "production" ) {
  console.log = function() {};
}
  • console.log out of the box has no support for direct printing to a file, though this could be done as shown by the snippet below. So calling logger.log('a') will log to the output file and logger.error('a') will log to the error file.
const fs = require("fs");
const output = fs.createWriteStream("./logs/output"); 
const error = fs.createWriteStream("./logs/error");

const logger = new Console.log({stdout: output, stderr: error})
  • console.log does not give you the ability to specify what you want to log, at a specific situation, for example, developers need logs for reasons different from that of the QA team, and console.log out of the box does not help with allowing one switch on what to log at a certain scenario

  • Lastly, console.log is lacking when it comes to giving information pertaining to the data logged like the time of logging, how long it took for the process to run, the log level, basic HTTP information, etc. that exist in some loggers

Node.js logging libraries

What logging modules are available for nodeJs? We will look at four in no particular order:

  • debug: It is a tiny javascript utility modeled after the Node.js debugging technique, works in Node.js and web browsers. it does not just display the data but adds colors to the output displayed as well as the time in milliseconds spent between two debug calls meaning that if it is passed before a block code and after, when we run the program it returns the amount of time it takes to run the program or the time debug was called. To read more about debug click here

  • morgan: A Node.js HTTP request logger middleware that can log both to file and also the stdout, it allows you to decide what information to log by using the existing format and also to define yours. To read more about morgan click here

  • winston: It is designed to be a simple and universal library, that can log to a database, a file, and to the stdout. It can be configured to log to all this at the same time. To read more about winston click here.

  • pino: A logger that is five times faster than it's alternative according to its benchmark result, it allows you to send its logs to datastores or webpages through the use of its various transport tools. To read more about pino click here.

PS. Cover image by Free-Photos from Pixabay