Skip to content
the poor coder

morgan log with customise

Poor Man
Published
Reading time
2 min read

Morgan Log with Customise#

As a developer, logging is an essential part of debugging and monitoring our applications. One of the popular logging libraries in Node.js is Morgan, which makes it easy to log HTTP requests. However, sometimes we may want to customize the format and content of the logs to our specific needs.

One way to customize Morgan logs is by using the format option, which allows us to define a custom log format string. This string can include placeholders that will be replaced with values from the request object.

Text

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

const app = express();

// Define custom log format
const logFormat = ':method :url :status :response-time ms - :res[content-length]';

// Use Morgan middleware with custom log format
app.use(morgan(logFormat));

app.get("/", (req, res) => {
  res.send("Hello World!");
});

app.listen(3000, () => {
  console.log("Server listening on port 3000");
});

In this example, we define a custom log format string that includes placeholders for the HTTP method, URL, status code, response time, and response content length. We then pass this format string to the Morgan middleware using the format option.

Another way to customize Morgan logs is by using the stream option, which allows us to redirect the logs to a custom output stream. This can be useful if we want to store the logs in a database or send them to a third-party logging service.

Text

const express = require("express");
const morgan = require("morgan");
const fs = require("fs");

const app = express();

// Define custom log stream
const logStream = fs.createWriteStream(__dirname + "/access.log", { flags: "a" });

// Use Morgan middleware with custom log stream
app.use(morgan("combined", { stream: logStream }));

app.get("/", (req, res) => {
  res.send("Hello World!");
});

app.listen(3000, () => {
  console.log("Server listening on port 3000");
});

In this example, we create a custom log stream using the fs.createWriteStream() method and pass it to the Morgan middleware using the stream option. This will redirect all the logs to the specified file.

In conclusion, Morgan is a powerful logging library that can be easily customized to fit our specific needs. Whether we want to change the log format or redirect the logs to a custom output stream, Morgan has us covered.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC