DEV Community

Cover image for ✨ Morgan Package in Nodejs 🔥
TAQUI ⭐
TAQUI ⭐

Posted on • Updated on

✨ Morgan Package in Nodejs 🔥

Welcome new Node.js learners! My name is Md Taqui Imam i am a Full Stack developer and

Follow me in Github

Today, In this blog post, I will explain how to use the Morgan logging package to log requests in your Node.js applications. This will help you debug and monitor your apps better.

What is Morgan?🤔

Morgan is a popular HTTP request logger middleware for Node.js. It logs details about incoming requests to your server and outputs them to the console. This helps you inspect traffic and debug issues.

Morgan is available as an NPM package and can be installed using:

Morgan NPM Package

npm install morgan
Enter fullscreen mode Exit fullscreen mode

Why Use Morgan for Logging?😮

  • It's simple and easy to set up in your app
  • Provides insight into your server traffic and requests
  • Lets you log different request details like method, URL, status code, etc.
  • Useful for debugging and monitoring app performance
  • Helps track down issues and errors
  • Many formatting options available for custom logging

How to Use Morgan Logging 👇

1. Install Morgan Package

First install Morgan using NPM:

npm install morgan
Enter fullscreen mode Exit fullscreen mode

2. Import and Initialize Morgan ⚙

Then in your main server file, import and initialize Morgan:

const morgan = require('morgan');
Enter fullscreen mode Exit fullscreen mode

3. Set Logging Format 💻

Next decide the logging format. Morgan has some common presets like combined, common, etc.

For example, to use the combined format:

app.use(morgan('combined')); 
Enter fullscreen mode Exit fullscreen mode

This will log details in Apache combined log format.

4. Place Morgan Middleware in App 🛠

Now place the morgan middleware in your Express app:

const express = require('express');
const app = express();

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

// Rest of app
Enter fullscreen mode Exit fullscreen mode

Morgan will now log all incoming requests to your app!

The logging will look something like:

127.0.0.1 - - [23/Apr/2020:10:27:08 +0000] "GET /hello HTTP/1.1" 200 35 "-" "Mozilla/5.0"
Enter fullscreen mode Exit fullscreen mode

This shows the client IP, date, requested URL, status code, response size, etc.

5. Use Custom Tokens for More Details 📝

You can also create a custom Morgan logging format with tokens to log specific data points you want:

app.use(morgan(':method :url :status :res[content-length] - :response-time ms'));
Enter fullscreen mode Exit fullscreen mode

This will log the method, URL, status code, response length and response time.

That covers the basics of setting up Morgan for logging in your Node.js and Express apps! Refer to their docs for more advanced usage.

Conclusion ✨

Logging requests with Morgan gives you useful insights into your app traffic. It's easy to set up, customize and great for debugging issues.

Other helpful logging packages are Winston and Bunyan. But Morgan is simpler and great for basic request logging in Node.js apps.

I hope this tutorial helped you understand Morgan logging! You can now quickly add professional request logging to monitor and troubleshoot your Node.js backend apps.

Follow me in Github

Happy Coding😊

Top comments (0)