logger

lib/middleware/logger.js

Log requests with the given options or a format string.

Options

  • format Format string, see below for tokens
  • stream Output stream, defaults to stdout
  • buffer Buffer duration, defaults to 1000ms when true
  • immediate Write log line on request instead of response (for response times)

Tokens

  • :req[header] ex: :req[Accept]
  • :res[header] ex: :res[Content-Length]
  • :http-version
  • :response-time
  • :remote-addr
  • :date
  • :method
  • :url
  • :referrer
  • :user-agent
  • :status

Formats

Pre-defined formats that ship with connect:

  • default ':remote-addr - - [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"'
  • short ':remote-addr - :method :url HTTP/:http-version :status :res[content-length] - :response-time ms'
  • tiny ':method :url :status :res[content-length] - :response-time ms'
  • dev concise output colored by response status for development use

Examples

 connect.logger() // default
 connect.logger('short')
 connect.logger('tiny')
 connect.logger('dev')
 connect.logger(':method :url - :referrer')
 connect.logger(':req[content-type] -> :res[content-type]')
 connect.logger(function(req, res){ return 'some format string' })

Defining Tokens

To define a token, simply invoke connect.logger.token() with the name and a callback function. The value returned is then available as ":type" in this case.

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

Defining Formats

All default formats are defined this way, however it's public API as well:

  connect.logger.format('name', 'string or function')

.token()

Define a token function with the given name, and callback fn(req, res).

.undefined()

Define a fmt with the given name.