A Complete Guide on Making the Most of Morgan Middleware

OMJI VERMA
3 min readFeb 4, 2023

--

Photo by Gabriel Heinzer on Unsplash

Introduction

A well-liked logging library for Node.js and Express.js is Morgan middleware. It assists in tracking the requests and responses that the server receives. Express.js relies heavily on middleware because it serves as a link between the request and response. This blog post’s goal is to clarify the value of using Morgan middleware in your Express.js applications as well as how to get the most out of it.

Setting up Morgan Middleware

The first step in using Morgan middleware is to install it. You can do this by running the following command in your terminal:

The first step in using Morgan middleware is to install it. You can do this by running the following command in your terminal:

npm install morgan

Once you have installed Morgan, you need to import it into your Express.js application. You can do this by requiring it in your server file:

const morgan = require('morgan');

Morgan middleware provides several configuration options for customising logs. You can customize the log format, the level of detail you want to see, and the logging stream.

Basic Usage of Morgan Middleware

To set up the basic Morgan middleware, you can use the following code:

app.use(morgan('combined'));

This changes the log format to combined, resulting in a detailed log of the request and response. The various logging formats are documented in the Morgan documentation.

Logging Format for Morgan Middleware

Morgan middleware offers several logging formats, each with varying levels of detail in the logs. The following are some of the most common logging formats:

  1. Combined Log Format (“combined”) — logs are written in the following format:: remote-address - remote-user [date] "method URL HTTP/version" status code "referrer" "user-agent"
  2. Common Log Format (“common”) — logs are written in the following format:: remote-address - remote-user [date] "method URL HTTP/version" status code
  3. Dev Log Format (“dev”) — logs are written in the following format:: method URL status code - response-time ms
  4. Short Log Format (“short”) — logs are written in the following format:: method URL status code
  5. Tiny Log Format (“tiny”) — logs are written in the following format:: method URL

Best Practices for using Morgan Middleware

When using Morgan middleware, it is essential to choose the correct logging format. This ensures that you receive the appropriate amount of information without overloading your logs. Consider the performance implications of using Morgan, as it may have an impact on the overall performance of your application. Caching logs for future use could also be a useful feature to include in your application.

Conclusion

When using Morgan middleware, it is essential to choose the correct logging format. This ensures that you receive the appropriate amount of information without overloading your logs. Consider the performance implications of using Morgan, as it may have an impact on the overall performance of your application. Caching logs for future use could also be a useful feature to include in your application.

--

--

OMJI VERMA
0 Followers

ECE Graduate and Self-Taught Web Developer with a Passion for Data Science.