Best Practices For Logging In Production For A Node.js Application

There are several things you can do to set up logging in your Node.js server application. You'd want to be able to save your logs in a file so that you can keep track of them and also have a way to display logs only for debugging purposes. Here are the best and simplest way to achieve those.

No console.log()

You might have been using console.log() the entire time to log or debug your program. I know I have been using it. However, you should be using it sparingly, or even better not at all. The reason for that is that console.log() is synchronous and is slow (printing in a console generally is)

Node.js applications run processes in a single thread. You would want to avoid synchronous calls as much as possible as they lock up the process. That means is that while a synchronous function is called (such as console.log), the program doesn't continue to run until the function completes running. Therefore, you'd want to make sure to avoid the logger from locking up your application, especially in a production environment.

As for speed, a simple illustration for the performance difference is as the following:

No logging (2.487ms)

console.time();

for (let i = 0; i < 1000000; i += 1) {
    // Empty
}

console.timeEnd();

With logging (7527.677ms)

console.time();

for (let i = 0; i < 1000000; i += 1) {
    console.log(i);
}

console.timeEnd();

Debug in development

Using console.log() in a development environment is completely fine as you'd want to use it to debug your application and you really won't be needing such fine-tuned performance gains. The trick is to get the logs to only show when you need to debug them.

Method 1: Debug package

A quick and simple way is to use the debug package.

debugnpm

It's basically a decorated console.log() that provides you with a simple way to toggle your debug logs. There are plenty of features in the package but simply put, you just have to give a name to your module and toggle the logs when based on what you need. Therefore, the logs are turned off by default and you are not going to have them run in your application when you are not debugging.

Method 2: Manual disable console.log() in production

A simpler and arguably more elegant solution is to override the console.log() function in production environments. You can do so with the following:

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

File logging

To log in a production environment, you'd want to log into a file in background. Thus, you won't need to logs to output on the console and indirectly cause performance hit. There are several logging libraries out there you can use and would depend largely on your preferences. Personally, I like to use winston for its simplicity but there are others use can use that provide more advanced features that you might like. Other alternatives you can check out are morgan and bunyan.

Winston

Best for general use. Easy to understand and simple to use.

Morgan

Great for access logs to keep track of client connections. Can be used for general logging as well.

Bunyan

Outputs in JSON format. Could be useful if you have a use case for that.

Wei-Ming Thor

I create practical guides on Software Engineering, Data Science, and Machine Learning.

Creator of ApX Machine Learning Platform

Background

Full-stack engineer who builds web and mobile apps. Now, exploring Machine Learning and Data Engineering. Read more

Writing unmaintainable code since 2010.

Skill/languages

Best: JavaScript, Python
Others: Android, iOS, C, React Native, Ruby, PHP

Work

Engineering Manager

Location

Kuala Lumpur, Malaysia

Open Source
Support

Turn coffee into coding guides. Buy me coffee