3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Build Status][travis-image]][travis-url]
6 [![Test Coverage][coveralls-image]][coveralls-url]
7 [![Gratipay][gratipay-image]][gratipay-url]
9 HTTP request logger middleware for node.js
11 > Named after [Dexter](http://en.wikipedia.org/wiki/Dexter_Morgan), a show you should not watch until completion.
16 var morgan = require('morgan')
19 ### morgan(format, options)
21 Create a new morgan logger middleware function using the given `format` and `options`.
22 The `format` argument may be a string of a predefined name (see below for the names),
23 a string of a format string, or a function that will produce a log entry.
27 Morgan accepts these properties in the options object.
31 Write log line on request instead of response. This means that a requests will
32 be logged even if the server crashes, _but data from the response (like the
33 response code, content length, etc.) cannot be logged_.
37 Function to determine if logging is skipped, defaults to `false`. This function
38 will be called as `skip(req, res)`.
41 // EXAMPLE: only log error responses
43 skip: function (req, res) { return res.statusCode < 400 }
49 Output stream for writing log lines, defaults to `process.stdout`.
51 #### Predefined Formats
53 There are various pre-defined formats provided:
57 Standard Apache combined log output.
60 :remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
65 Standard Apache common log output.
68 :remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length]
73 Concise output colored by response status for development use. The `:status`
74 token will be colored red for server error codes, yellow for client error
75 codes, cyan for redirection codes, and uncolored for all other codes.
78 :method :url :status :response-time ms - :res[content-length]
83 Shorter than default, also including response time.
86 :remote-addr :remote-user :method :url HTTP/:http-version :status :res[content-length] - :response-time ms
94 :method :url :status :res[content-length] - :response-time ms
99 ##### Creating new tokens
101 To define a token, simply invoke `morgan.token()` with the name and a callback function. This callback function is expected to return a string value. The value returned is then available as ":type" in this case:
103 morgan.token('type', function(req, res){ return req.headers['content-type']; })
106 Calling `morgan.token()` using the same name as an existing token will overwrite that token definition.
110 The current date and time in UTC. The available formats are:
112 - `clf` for the common log format (`"10/Oct/2000:13:55:36 +0000"`)
113 - `iso` for the common ISO 8601 date time format (`2000-10-10T13:55:36.000Z`)
114 - `web` for the common RFC 1123 date time format (`Tue, 10 Oct 2000 13:55:36 GMT`)
116 If no format is given, then the default is `web`.
120 The HTTP version of the request.
124 The HTTP version of the request.
128 The Referrer header of the request. This will use the standard mis-spelled Referer header if exists, otherwise Referrer.
132 The remote address of the request. This will use `req.ip`, otherwise the standard `req.connection.remoteAddress` value (socket address).
136 The user authenticated as part of Basic auth for the request.
140 The given `header` of the request.
144 The given `header` of the response.
148 The time between the request coming into `morgan` and when the response headers are written, in milliseconds.
152 The status code of the response.
156 The URL of the request. This will use `req.originalUrl` if exists, otherwise `req.url`.
160 The contents of the User-Agent header of the request.
166 Simple app that will log all request in the Apache combined format to STDOUT
169 var express = require('express')
170 var morgan = require('morgan')
174 app.use(morgan('combined'))
176 app.get('/', function (req, res) {
177 res.send('hello, world!')
181 ### vanilla http server
183 Simple app that will log all request in the Apache combined format to STDOUT
186 var finalhandler = require('finalhandler')
187 var http = require('http')
188 var morgan = require('morgan')
190 // create "middleware"
191 var logger = morgan('combined')
193 http.createServer(function (req, res) {
194 var done = finalhandler(req, res)
195 logger(req, res, function (err) {
196 if (err) return done(err)
198 // respond to request
199 res.setHeader('content-type', 'text/plain')
200 res.end('hello, world!')
205 ### write logs to a file
207 Simple app that will log all request in the Apache combined format to the file "access.log"
210 var express = require('express')
211 var fs = require('fs')
212 var morgan = require('morgan')
216 // create a write stream (in append mode)
217 var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'})
220 app.use(morgan('combined', {stream: accessLogStream}))
222 app.get('/', function (req, res) {
223 res.send('hello, world!')
227 ### use custom token formats
229 Sample app that will use custom token formats. This adds an ID to all requests and displays it using the `:id` token.
232 var express = require('express')
233 var morgan = require('morgan')
234 var uuid = require('node-uuid')
236 morgan.token('id', function getId(req) {
243 app.use(morgan(':id :method :url :response-time'))
245 app.get('/', function (req, res) {
246 res.send('hello, world!')
249 function assignId(req, res, next) {
259 [npm-image]: https://img.shields.io/npm/v/morgan.svg?style=flat
260 [npm-url]: https://npmjs.org/package/morgan
261 [travis-image]: https://img.shields.io/travis/expressjs/morgan.svg?style=flat
262 [travis-url]: https://travis-ci.org/expressjs/morgan
263 [coveralls-image]: https://img.shields.io/coveralls/expressjs/morgan.svg?style=flat
264 [coveralls-url]: https://coveralls.io/r/expressjs/morgan?branch=master
265 [downloads-image]: https://img.shields.io/npm/dm/morgan.svg?style=flat
266 [downloads-url]: https://npmjs.org/package/morgan
267 [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg?style=flat
268 [gratipay-url]: https://www.gratipay.com/dougwilson/