Production Logging in Nodejs

Wajiha Abid
4 min readJul 21, 2022

In development we can easily debug with consoling everything but what about debugging application in production. Ever think how to handle it efficiently or go through application each feature to track the problem?. For this purpose, lot of library are already in the market, for example bunyan, log4js ,Winston etc. These are the logging libraries which has made debugging easier in production level applications.

Why we are using winston?

Winston is general logging library not just specific to nodejs. It come up with lot of options like connect directly to databases, streams or static files etc. You can explore it in detail in the documentation. We are using winston because it is easy to configure and due to its multiple storage methods.

Pre-requisites:

  • Nodejs
  • Express

So lets configure it in simple 3 steps:

Step 1:

Setup the basic express server and install the winston library.

npm i winston

Step 2:

create a logger folder in the root directory and make a file named winston.config.js. It will have a following directory structure.

Now lets start configuring our custom logger. Winston provides you with the function createLogger which will take option some options like the format, transport, log level etc. Transport is the link to the storage where your logs will be stored. In the transport you define either you want to store in database, streams or static files.

**logger/winston.config.js//custom logger
const customLogger = () => {
return createLogger({
format: combine(
timestamp(),
format.json()
),
transports: [
new transports.File({ filename: ‘winston.log’ })
]
});
}

In the above code, lets go through each property one by one

  • level = defines the type of logging like warn, info etc.
  • format = the way you need your logs to be stored. In above it is set to the json format.
  • transports = configure the storage where the logs will be stored.

You can specify the format you want by defining your own format by using printf option in the winston library. We have used the following format to save our logs:

**logger/winston.config.jsconst { combine, timestamp, printf } = format;//my custom format
const myFormat = printf(({ level, message, timestamp, …body }) => {
let log = `${timestamp} [${level}] ${message} \n`;
log += “Request:” + JSON.stringify(body.request) + “\n”
log += “Response:” + JSON.stringify(body.response) + “\n”
log += “-”.repeat(100)
return log;
});
//custom logger
const customLogger = () => {
return createLogger({
format: combine(
timestamp(),
myFormat
),
transports: [
new transports.File({ filename: ‘winston.log’ })
]
});
}

STEP 3:

Export this logger in any file and use it as you want. For example I have used it in my index file just to check.

**index.jsconst logger = require(“./logger/winston.config”);logger.info({
message: “hello”,
request: { url: “abc/hello”, body: { a: 1, b: ‘hello’ } },
response: { success: true, data: [], message: “added!”, statusCode: 200 }
})
logger.error({
message: “hello”,
request: { url: “abc/hello”, body: { a: 1, b: ‘hello’ } },
response: { success: false, data: [], message: “error in adding!”, statusCode: 400 }
})

And the log file will look like this according to our format:

So this how we configure the winston, now one scenario occur in the apps with heavy traffics that it make alot of files or in a single file to many logs make a mess and become difficult to track. So cater up that we can use a library named winston-daily-rotate-file. Its a transport on winston which logs to a rotating file. Logs can be rotated based on a date, size limit, and old logs can be removed based on count or elapsed days. You just have to replace it in the transports.

Conclusion:

Logging is a very main part of an application if you are not using any cloud to deploy your application because in cloud, logs are already being maintained. If logs have to be monitored manually it is very important that logs should be clean and concise, no need to add garbage data in it otherwise it would be pain to monitor your application in production.

--

--