logging in Node.JS – Express Application using Winston logger

Logging is keeping a record of all data input, errors, data output, and final results in a program. While developing an application, keeping track of responses, requests, errors, etc makes the development process more efficient. Keeping logs is also a part of code standardization.

Logs can be displayed on the console and written to a file while working with Node.js.Here in this tutorial, we will learn how to create create a logger which keeps track of Request, Response, Errors and Time while building an ExpressJS application in Javascript/Typescript


Step 1

Install the Winston node package with the Node package manager


Step 2

Create a logger.ts (.js file if you are working with  Javascript) preferably in a common folder outside, and add the following code to it


In the above code, createLogger creates a new logger



Adds Timestamp to your log. It defines formatting options


Displays the log in console.


Writes your error file to error.log and info to info.log



If false, handled exceptions will not cause process.exit


Step 3

In your controller or where ever you want to create the log, import the logger file

Step 4

Now we will see how to add logs to the function which handles the request and response.

We use logger.log function to add a log.

logger.log accepts an object with details.

A basic log will look like this


Now we will create a detailed log for our express application

The details include 

Operation – The purpose of the request

level – It is a mandatory field, error level (info/error)

message – Message to be logged

transationId – Identify the current transaction

correlationId –  Identify the user across communicating systems. Preferably a unique identifier of requesting user

request.query – the request parameters


The log response will look like this.


The different levels of logs usually implemented while developing APIs are are


Trace  –   Trace level logging used to identify which part of the execution of the code is reached. Should provide minimal yet accurate information regarding which part of the code is executed.
Debug  –   Log which will contain adequate information for debugging an error. Usually logged inside an error catch
Info   –   General yet valuable information like starting of server in a port or start of an operation
Warn   –   Anything that can potentially cause some odd behavior while doing an operation This won’t break the application. The log will have information regarding the reason that a change in behavior can occur.
Error  – Error is something that will be fatal to the operation but will not break the application as a whole. It will not return the desired result from the operation but returns an error. For example, calling external service from a function or File operation with file missing
Fatal –   Any error that can cause the app to break. A fatal error will cause the application to stop running.API running on a port will stop working on fatal error
Warning, info, debug, notice, error, emerg, alert, critic are the levels provided by Winston logger for logging
For more information read the documentation here https://www.npmjs.com/package/winston.

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *