Express JS morgan


morgan is a middleware in Express.js used for logging HTTP requests. It logs information about each incoming request, which is useful for monitoring, debugging, and analyzing traffic in an application.

Key Features of morgan:

  1. Request Logging:

    • It logs details such as request method, URL, response status code, response time, and more.
    • Helps in understanding how users are interacting with your application and can also be used for performance analysis.
  2. Predefined Formats:

    • morgan provides several predefined formats that specify what information is logged. These formats are optimized for different use cases.
      • combined: Standard Apache combined log output, includes detailed information.
      • common: Standard Apache common log output, contains less detail than combined.
      • dev: Concise output colored by response status for development use.
      • short: Shorter than common, includes minimal but useful information.
      • tiny: Minimal logging with only the essential data.
  3. Custom Formats:

    • You can also define your own custom log format to log specific details based on your needs.
  4. Log Destination:

    • By default, logs are sent to the console, but you can configure morgan to write logs to a file or another logging service.

Installation:

To use morgan, you need to install it as a dependency in your Express.js project:

npm install morgan

Usage Example:

Here’s a simple usage of morgan in an Express.js application:

const express = require('express'); const morgan = require('morgan'); const app = express(); // Use 'dev' logging format in development app.use(morgan('dev')); app.get('/', (req, res) => { res.send('Hello, world!'); }); app.listen(3000, () => { console.log('Server is running on port 3000'); });

In this example, every time a request is made, morgan will log details about the request to the console.

Predefined Formats in Detail:

  • combined: Logs details in the following format:

    :remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"

    Example log:

    ::1 - - [10/Sep/2024:12:34:56 +0000] "GET /home HTTP/1.1" 200 1234 "-" "Mozilla/5.0"
  • dev: Outputs colored logs for development environments:

    GET /home 200 12.345 ms - 345
  • tiny: Minimal output:

    GET /home 200 12 - 345

Custom Logging Example:

You can also create a custom logging format by specifying your own tokens:

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

This will output logs like:

GET /home 200 345 - 12.345 ms

Why Use morgan?

  • Monitoring: It helps track how often specific routes are being accessed and the performance of these requests.
  • Debugging: In case of issues, morgan can help you pinpoint which requests are causing errors.
  • Analytics: By analyzing logs, you can get insights into traffic patterns and user behavior.

morgan is especially useful in development, but it can also be used in production for detailed request logging.