Logging in Node.js

Node.jsJavascriptWeb DevelopmentFront End Technology

Logging is a very essential part in any application whether it is made in Node.js or any other programming languages. Logging helps us to detect weird behaviours of an application along with real-time errors and exceptions. One should definitely put logical logs in their application. These logs help the user to identify any mistakes and resolve it on urgent basis.

There are 5 different log levels which are present at the moment with the user. These log levels are used to define different kinds of logs and helps the user to identify different scenarios. The log levels must be carefully configured to get the best deal out of these logs −

  • error

  • warn

  • info

  • verbose

  • debug


You can put this middleware in the request pipeline so that you can directly use the power of debug module to print logs. The best advantage of debug module is that almost all the modules use debug for printing logs. You can use the debug module to print logs and errors.

This middleware is mainly used for acting as a middle pipeline – you can pass logs in this pipeline which will be printed in the log file. Requests and responses are also passed through this middleware for logging purposes. It is very beneficial in an Express app. Setting up middleware is easy and can be done in any frameworks being used.

Configuring files for middleware


const app = express()
const logMiddleware = require('my-logging-middleware')


const router = express.Router()
const routeLoggingMiddleware = require('my-route-logging-middleware')


const app = express();
const errorLoggingMiddleware = require('my-error-logging-middleware')

Winston Package

You can also use the winston package for logging purposes. It also provides different log levels, queries as well as a profiler.


const app = express()
const winstonPackage = require('winston')
const consoleTransport = new winstonPackage.transports.Console()
const myWinstonOptions = {
   transports: [consoleTransport]
const logger = new winstonPackage.createLogger(myWinstonOptions)

function logRequest(req, res, next) {

function logError(err, req, res, next) {
Published on 20-May-2021 11:21:23