Records detailed information of each HTTP request, making it easier for developers to debug and monitor applications.
1. Installation
npm install morgan
2. Basic use
const express = require("express"); const morgan = require("morgan"); const app = express(); // Use the morgan middleware, the default log format is 'dev'(morgan("dev")); ("/", (req, res) => { ("Hello, World!"); }); const port = 3000; (port, () => { (`Server running on port ${port}`); });
3. Log format
`morgan` provides a variety of built-in log formats, and each format outputs different information.
1. `dev`
This is a commonly used format in the development environment. The output is simple and includes color coding for easy viewing. Sample output:
The meanings are: request method (`GET`), request path (`/`), response status code (`200`), response time (`3.029 ms`), and response content length (`12`).
GET / 200 3.029 ms - 12
2. `combined`
This is a common log format similar to Apache servers, including details such as client IP addresses, user agents, etc. Sample output:
::1 - - [24/Jan/2025:10:30:00 +0000] "GET / HTTP/1.1" 200 12 "http://localhost:3000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
3. `common`
It is similar to `combined` format, but does not contain user agent information. Sample output:
::1 - - [24/Jan/2025:10:30:00 +0000] "GET / HTTP/1.1" 200 12
4. `short`
The output information is a little more than `dev` format, but it is simpler than `combined` format. Sample output:
::1 - GET / HTTP/1.1 200 12 - 3.029 ms
5. `tiny`
The most concise log information is output, including only the request method, path, status code, response content length and response time. Sample output:
GET / 200 12 - 3.029 ms
4. Custom log format
Use the `()` method to define a custom log token `custom-date` to output the ISO format of the current time.
const express = require("express"); const morgan = require("morgan"); const app = express(); // Custom log format("custom-date", () => { return new Date().toISOString(); }); const customFormat = ":custom-date :method :url :status :res[content-length] - :response-time ms"; (morgan(customFormat)); ("/", (req, res) => { ("Hello, World!"); }); const port = 3000; (port, () => { (`Server running on port ${port}`); });
5. Write logs to file
`morgan` can also write log information to a file instead of just outputting it to the console.
const express = require("express"); const morgan = require("morgan"); const fs = require("fs"); const path = require("path"); const app = express(); // Create a write stream (append mode)const accessLogStream = ( (__dirname, ""), { flags: "a" } ); // Use morgan and write the log to a file(morgan("combined", { stream: accessLogStream })); ("/", (req, res) => { ("Hello, World!"); }); const port = 3000; (port, () => { (`Server running on port ${port}`); });
Here, use the `()` method to create a write stream, appending the log information of `morgan` to the `` file.
This is the end of this article about the detailed explanation of Morgan dependencies. For more related Morgan dependencies, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!