DEV Community

loading...
Cover image for Using morgan with Express for Backend Logging

Using morgan with Express for Backend Logging

Arifa
I learn new things to write about!
Updated on ・2 min read

Logging is an important element of writing software as it helps us to see the software operations, debug and catch errors.

morgan is a logging tool (middleware) that can be used in HTTP servers implemented using Express & Node.js. It can be used to log requests, errors, and more to the console.

Middleware are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next [1].

Getting started with morgan

Installation

$ npm install morgan --save
Enter fullscreen mode Exit fullscreen mode

A basic code snippet using morgan looks like this:

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

const app = express();

app.listen(3000, () => {
     console.log('App listening on port 3000 ...');
});
Enter fullscreen mode Exit fullscreen mode

Output

console.log('App listening on port 3000 ...');
Enter fullscreen mode Exit fullscreen mode

Formatting Logs

Making use of predefined format strings

morgan offers us with a set of predefined format strings, which are plug-and-play. For example, let's look at morgan('tiny'):

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

const app = express();
app.use(morgan('tiny'));

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

app.listen(3000, () => {
    console.log('App listening on port 3000 ...');
});
Enter fullscreen mode Exit fullscreen mode

Open your web browser and go to http://localhost:3000. You will see the following output in your web server:

App listening on port 3000 ...
GET / 200 24 - 2.868 ms
Enter fullscreen mode Exit fullscreen mode

The output is displayed in the following format:

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

You can find more such predefined formats from here.

Custom Log Formats by Creating Our Own Tokens

We can create our own custom log format strings. Below is an example of how we can create our own custom log format strings.

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

const app = express();

//we are defining a new parameter called host
morgan.token('host', function(req, res) {
    return req.hostname;
});

// we are using the host parameter
app.use(morgan(':method :host :status :res[content-length] - :response-time ms'))

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

app.listen(3000, () => {
    console.log('App listening on port 3000 ...');
});
Enter fullscreen mode Exit fullscreen mode

Output

App listening on port 3000 ...
GET localhost 200 21 - 2.819 ms
Enter fullscreen mode Exit fullscreen mode

I hope this post has helped you to get started with logging using morgan. Please like this post and follow me for more such posts.

References
[1] http://expressjs.com/en/guide/using-middleware.html#using-middleware

Discussion (0)