Why Use Morgan
In one sentence, morgan simplifies the task of logging HTTP requests to and from your application. What does this mean? Normally (without morgan), developers need to manually create all the logging logic. They need to instruct Node.js/Express.js what, how, and where to save. Morgan does this for you. It collects logs from your server and makes them ready to read. It also comes with a few built-in, predefined presets, saving you the time and effort of setting up all the logging yourself. It can be extremely helpful when bootstrapping a new project, but it’s also powerful, so it’s perfectly fine for bigger applications as well.
Getting Set Up
Morgan is installed via NPM, just like any other Node.js module:
npm install morgan
After that, you must tell Node.js to include Morgan in your app:
const morgan = require('morgan');
That concludes the fundamentals. Morgan is now up and running and ready to use.
Here’s how to incorporate it into your project:
"use strict";
const cds = require("@sap/cds");
const morgan = require('morgan');
cds.on("bootstrap", app => app.use(morgan('combined')));
module.exports = cds.server;
Log Output Format
Morgan’s logs may be customized to include exactly the information you require, which is one of its most useful features. You can format your logs in one of two ways:
- Manually by using Tokens
You can also easily build new ones if the pre-defined ones aren’t enough. - Pre-defined Log
This module already has a simple pre-configured set of items to log; all you have to do now is choose the combination that best meets your needs.
Manually by using Tokens
If you want to use the format function, you’ll need three arguments: tokens, req, and res. The HTTP request is req, and the HTTP response is res. A token is an object that contains all declared tokens. The function should return a string that will be the logline, or undefined/null if you don’t want to log anything.
When utilizing predefined tokens, keep in mind that they must always be declared as strings, with a colon before the token’s name :method.
Using a custom format function
app.use(morgan((tokens, req, res) => {
return [
tokens.method(req, res),
tokens.url(req, res),
tokens.status(req, res),
tokens.res(req, res, 'content-length'), '-',
tokens['response-time'](req, res), 'ms'
].join(' ')
}))
Using a format string of predefined tokens:
app.use(morgan(':method :url :status :res[content-length] - :response-time ms'))
Pre-defined Log
There are five predefined formats that you can utilize to quickly obtain the information you require. They are as follows:
- combined – This sets your logs to the Apache standard combined format
- common – Refers to the Apache common format standard
- dev – A color-coded log format (based on request status)
- short – Less than the normal format, with only a few items you’d expect to see in a request logline
- tiny – Even less, simply the reaction time and a few extras
Returning to the previous example, here’s how the module logs the same request in several formats:
The output of the ‘Combined’ format:
cds] - GET /catalog/Books
::ffff:127.0.0.1 - - [20/Jul/2022:06:56:28 +0000] "GET /catalog/Books HTTP/1.1" 200 136 "https://port4004-workspaces-ws-nqwd4.cry10.int.applicationstudio.cloud.sap/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36
The output of the ‘Dev’ format:
[cds] - GET /catalog/Books
GET /catalog/Books 200 7.971 ms - 136
The output of ‘Tiny’ format:
GET / 304 - - 5.483 ms
GET / 304 - - 0.839 ms
[cds] - GET /catalog/Books
GET /catalog/Books 200 136 - 33.608 ms
Summary
Morgan drastically simplifies logging for your Express.js applications, and it’s a powerful tool, allowing you to build custom logging formats. You can use morgan for small projects when you need quick and simple logging as well as for bigger applications when you need custom solutions. In this post, we covered how to install morgan, how to use built-in presets, and how to create your own. In most cases, you’ll only need to add two or three lines of code to start using morgan.