Skip to content

Implementing Logging in Node.js Express

In Node.js Express applications, logging is an essential aspect of monitoring and debugging. It allows you to capture important information about the application's behavior, errors, and requests. In this article, we will explore how to implement logging in a Node.js Express application, with logs being outputted both in the terminal and to a log file.

Prerequisites

Before we begin, make sure you have the following prerequisites installed:

  • Node.js
  • Express.js

Setting Up Dependencies

To get started, we need to install two packages: morgan and winston. The morgan package will help us log requests in the terminal, while winston will handle logging to a file.

Open your terminal and run the following command to install the packages:

bash
npm install morgan winston

Setting Up the Logging Infrastructure

First, let's import the required modules in our Express application file (app.js or server.js):

javascript
const express = require('express');
const morgan = require('morgan');
const winston = require('winston');
const fs = require('fs');
const path = require('path');

Next, we'll set up the log file and configure the log transport using the winston library. We'll create a log directory and a log file named app.log. Place the following code in your application file:

javascript
const logsDir = path.join(__dirname, 'logs');
fs.existsSync(logsDir) || fs.mkdirSync(logsDir);

const logFilePath = path.join(logsDir, 'app.log');

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  transports: [
    new winston.transports.File({ filename: logFilePath }),
  ],
});

With this configuration, log messages will be written to the app.log file.

Logging in the Terminal

To log requests in the terminal, we'll use the morgan middleware. Add the following code to your application file:

javascript
app.use(morgan('dev'));
app.use(morgan('combined', { stream: winston.stream }));

This code sets up morgan to log requests in the terminal using the 'dev' format and also pipes the log messages to the winston logger.

Logging in the Application

Now, you can use the logger to log messages throughout your application. Here's an example:

javascript

// Log an error
logger.error('An error occurred.');

// Log an informational message
logger.info('Request received.');

Feel free to log messages according to your application's specific needs.

Conclusion

In this article, we learned how to implement logging in a Node.js Express application. We set up logging infrastructure using winston, configured logging to both the terminal and a log file, and demonstrated how to log requests and custom messages. Having a comprehensive logging system in place helps in troubleshooting issues, monitoring application behavior, and maintaining the health of your Node.js Express application.

Remember to adjust log levels, log formats, and log file paths based on your requirements. Additionally, you can explore further features and options provided by the winston library to enhance your logging implementation.

I hope this article helps you in implementing logging effectively in your Node.js Express applications!