DEV Community

Sumit negi
Sumit negi

Posted on

Understanding Morgan

In modern web development, understanding how your application behaves under different circumstances is crucial. One of the essential tools for this is logging, which provides insights into the inner workings of your code. In Node.js and Express applications, logging HTTP requests is made easy and efficient with the Morgan package.

What is Morgan?

Morgan is a logger package used as middleware in Node.js and Express applications. It is designed to log detailed information about HTTP requests, including the request method, URL, status code, response time, and more. This information is invaluable for debugging, monitoring, and analyzing the performance of your application.

Installation and Basic Usage

To get started with Morgan, you first need to install it using npm or yarn:

npm install morgan
Enter fullscreen mode Exit fullscreen mode

Once installed, you can include Morgan in your Express application like this:

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

const app = express();

// Use Morgan middleware for logging in development mode
app.use(morgan('dev'));

// Your routes and other middleware
// ...

// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});
Enter fullscreen mode Exit fullscreen mode

In the code above, morgan('dev') specifies the logging format as 'dev', which is one of the predefined formats provided by Morgan. This format includes the request method, URL, status code, response time, and more.

Customizing Logging Formats

Morgan allows you to customize logging formats based on your specific requirements. You can define custom tokens using morgan.token and include them in your logging format. Here's an example of defining a custom token for the host:

morgan.token('host', function(req, res) {
  return req.hostname;
});

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

In this example, :host is a custom token that logs the hostname of the request. You can create more custom tokens to log additional information as needed.

Designing Tokens with Custom Arguments

Morgan also allows you to design tokens with custom arguments, providing even more flexibility in logging. You can use square brackets to define arguments passed to a token. Here's an example of using a custom token with an argument:

morgan.token('param', function(req, res, param) {
  return req.params[param];
});

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

In this example, :param[id] is a custom token that logs the value of the id parameter in the request URL. You can replace id with any parameter name to log its value dynamically.

Using Morgan in Production

While Morgan is extremely useful during development for debugging and monitoring, it's important to configure it appropriately for production environments. In production, you may want to use a more concise and structured logging format to reduce the amount of log data generated. Additionally, you should ensure that sensitive information such as request bodies or headers are not logged to avoid security risks.

Here's an example of configuring Morgan for production:

const fs = require('fs');
const path = require('path');

const accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' });

// Use Morgan middleware with a custom logging format for production
app.use(morgan(':date[iso] :method :url :status :response-time ms', { stream: accessLogStream }));
Enter fullscreen mode Exit fullscreen mode

In this configuration, Morgan logs the date in ISO format, request method, URL, status code, response time, and more to a file named access.log. This setup ensures that logging in production is structured and saves logs to a file for later analysis.

Conclusion

Morgan is a powerful logging middleware for Node.js and Express applications, providing detailed insights into HTTP requests. By customizing logging formats and tokens, you can tailor the logging output to suit your specific needs. Whether in development or production, using Morgan effectively enhances your ability to debug, monitor, and analyze the performance of your application.

Top comments (0)