The Use and Interpretation of morgan Log

Posted by MMeticulous on Wed, 02 Oct 2019 15:59:54 +0200

morgan

// Introducing morgan package
var morgan = require('morgan')

Usage method

morgan(format,options)

The format: (string/function) printing mode can be the name of the predefined printing method, or the formatted string, or the callback method of the formatted entry.

  • Use predefined printing
morgan('tiny');
  • Use formatted strings
morgan(':method :url :status :res[content-length] - :response-time ms')
  • Using formatted callback method
morgan(function (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(' ')
})

options: (object) log printing parameter configuration

  • immediate: print when request arrives
  • skip: Set logs that ignore printing
morgan('combined', {
  skip: function (req, res) { return res.statusCode < 400 }
})
  • Stream: Output stream, default console output
  • Predefined Formats: Predefined Printing Format

Predefined format

combined: Standard Apache Composite Log Output

:remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"

common: Standard Apache public log output

:remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length]

dev: Color output log based on the returned status code

:method :url :status :response-time ms - :res[content-length]

short: concise output with response time

:remote-addr :remote-user :method :url HTTP/:http-version :status :res[content-length] - :response-time ms

tiny: console output

:method :url :status :res[content-length] - :response-time ms

token custom format output

morgan.token('type', function (req, res) { return req.headers['content-type'] })

date[format] date format

  • clf: "10/Oct/2000:13:55:36 +0000"
  • iso: "2000-10-10T13:55:36.000Z"
  • web "Tue, 10 Oct 2000 13:55:36 GMT"

http-version http version

method request type

Reference headers for referrer requests

remote-addr remote request address

remote-user remote user

req[header] request header information'-'delimitation

response-time[digits] request response time

status Response status Code

url access address

user-agent browser information

examples

  • Standard Apache Composite Log Output
var express = require('express')
var morgan = require('morgan')
var app = express()
app.use(morgan('combined'))
app.get('/', function (req, res) {
  res.send('hello, world!')
})
  • Export the log to the access.log log 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!')
})
  • Generate log files every day and place them in the established directory
var express = require('express')
var fs = require('fs')
var morgan = require('morgan')
var path = require('path')
var rfs = require('rotating-file-stream')
 
var app = express()
var logDirectory = path.join(__dirname, 'log')
 
// ensure log directory exists
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
 
// create a rotating write stream
let accessLogStream = FileStreamRotator.getStream({
  date_format: 'YYYYMMDD', // Date format
  filename: path.join(logDirectory, 'morgan_%DATE%.log'), //Log file name
  frequency: 'daily', //
  verbose: false
})
 
// setup the logger
app.use(morgan('combined', { stream: accessLogStream }))
 
app.get('/', function (req, res) {
  res.send('hello, world!')
})

Topics: Apache