Link Search Menu Expand Document

Options

  • immediate : write log line on request (but can’t log response)
  • skip: (req, res): bool, determine if logging should be skipped
  • fomrats:
    • combined
    • common
    • dev

Plugins

  • morgan-json: print json formatted morgan logs

β€” Quickstart

var morgan = require('morgan')


// pre-defined
morgan('tiny')

Cook

Write to file

var express = require('express')
var fs = require('fs')
var morgan = require('morgan')
var path = require('path')
 
var app = express()
 
// create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' })
 
// setup the logger
app.use(morgan('combined', { stream: accessLogStream }))
 
app.get('/', function (req, res) {
  res.send('hello, world!')
})

Table of contents


Copyright © 2020 Thence LLC