Why Console Logs Can Harm Your Production Apps

Welcome to my mini blog series where I will post bite sized blogs which
are easy to consume and will give some knowledge to the readers.

Today we will talk about a problem which I have personally encountered
in my own software development journey.

I want to talk about how console.logs can affect your app in a production environment.

The major reason to carefully use console.logs in production apps
is because its a synchronous operation. It blocks the execution of the program
until the logging operation is completed.

Imagine how detrimental the effect will be if we are using it in a loop.

You can try this by logging in a long running for loop it will most likely
crash the program.

Its an I/O operation hence they are relatively slow, and the more logs you generate, the more system resources (CPU, memory, disk, or network bandwidth) are consumed, potentially causing a bottleneck in your application.

If PM2 service is used to maintain the node application then the console.logs
are usually written by default in access.log file which is maintained by pm2.

If logging is not done cautiously then access.log file will keep on accumulating
space reducing overall disk space which can crash the server due to lack of space. This is something I have personally encountered.

Now let’s see what are some steps that we can take to avoid and overcome this problem.

1. If there are console.logs used already and they are present in multiple files
we can override the console.log method itself to prevent the problem.
Below is example to do that. We can do this in app.js/index.js basically the entry point of the nodejs application.

if (process.env.NODE_ENV === 'production') {
  console.log = function() {};
}

2. You can also use a dedicated library like winston to manage your logs. Winston provide lot of capabilities and ways to better manage the logs.

Winston manages the logging asynchronously which is a major difference compared to console.logs.

asynchronous logging helps as it does not block the main thread and other process can still continue without waiting for the logging to get completed.

It also supports log formatting where you can format color, JSON , timestamps etc.

It supports multiple transport so that you can configure to save your logs to multiple places.

winston supports log rotation, which automatically creates new log files after a certain size or time limit. This is critical in production environments where logs can grow quickly and eat up disk space.

3. If you are still continuing with console.logs you can still use PM2’s feature of log rotation. In log rotation when the log files reaches certain file size
the older logs in the same file will be deleting making way for more
logs to be added without increasing the server or the file size.

4. Another point is log less frequently as possible. Log where it is absolutely needed and critical. Avoid logging in a loop. Avoid logging large data structures.

If you liked what you read do give the post a like and comment down you views.

If you want to read more of my blogs you can check out my hashnode profile
where I have posted more node.js related blogs.

https://saishborkar.hashnode.dev/