DEV Community

Margaret W.N
Margaret W.N

Posted on

Getting Started with Morgan

Morgan is a middleware function for logging information about the http request/response in a server application.

Installation

$ npm install morgan
Enter fullscreen mode Exit fullscreen mode

Use

const morgan = require('morgan');

app.use(morgan('dev'));
Enter fullscreen mode Exit fullscreen mode

Arguments.

Morgan takes two arguments: format and options.
FORMATS
You can defined your own format string or use the predefined formats. I like to keep things simple while learning a new concept hence i opted for the predefined formats. Here are a few predefined formats.

  • tiny - logs out minimal information about the request. status.
app.use(morgan('tiny'));
Enter fullscreen mode Exit fullscreen mode
  • dev logs out concise output with color coded status.
app.use(morgan('dev'));
Enter fullscreen mode Exit fullscreen mode
  • combined logs out Standard Apache combined log output (A lot of information that you probably don't care about).
app.use(morgan('combined'));
Enter fullscreen mode Exit fullscreen mode

For more formats refer to the documentation.

OPTIONS
Valid option (objects) accepted by morgan.

  • immediate - information is logged on request instead of on response
  • skip - determines if logging is skipped
  • stream - Output stream for writing log lines

More on this in the documentation

Day 39

Top comments (0)