morgan
Advanced tools
Comparing version 1.5.3 to 1.6.0
@@ -0,1 +1,26 @@ | ||
1.6.0 / 2015-06-12 | ||
================== | ||
* Add `morgan.compile(format)` export | ||
* Do not color 1xx status codes in `dev` format | ||
* Fix `response-time` token to not include response latency | ||
* Fix `status` token incorrectly displaying before response in `dev` format | ||
* Fix token return values to be `undefined` or a string | ||
* Improve representation of multiple headers in `req` and `res` tokens | ||
* Use `res.getHeader` in `res` token | ||
* deps: basic-auth@~1.0.2 | ||
- perf: enable strict mode | ||
- perf: hoist regular expression | ||
- perf: parse with regular expressions | ||
- perf: remove argument reassignment | ||
* deps: on-finished@~2.3.0 | ||
- Add defined behavior for HTTP `CONNECT` requests | ||
- Add defined behavior for HTTP `Upgrade` requests | ||
- deps: ee-first@1.1.1 | ||
* pref: enable strict mode | ||
* pref: reduce function closure scopes | ||
* pref: remove dynamic compile on every request for `dev` format | ||
* pref: remove an argument reassignment | ||
* pref: skip function call without `skip` option | ||
1.5.3 / 2015-05-10 | ||
@@ -2,0 +27,0 @@ ================== |
402
index.js
@@ -6,7 +6,19 @@ /*! | ||
* Copyright(c) 2014 Jonathan Ong | ||
* Copyright(c) 2014 Douglas Christopher Wilson | ||
* Copyright(c) 2014-2015 Douglas Christopher Wilson | ||
* MIT Licensed | ||
*/ | ||
'use strict' | ||
/** | ||
* Module exports. | ||
* @public | ||
*/ | ||
module.exports = morgan | ||
module.exports.compile = compile | ||
module.exports.format = format | ||
module.exports.token = token | ||
/** | ||
* Module dependencies. | ||
@@ -20,2 +32,3 @@ * @private | ||
var onFinished = require('on-finished') | ||
var onHeaders = require('on-headers') | ||
@@ -48,29 +61,32 @@ /** | ||
exports = module.exports = function morgan(format, options) { | ||
if (typeof format === 'object') { | ||
options = format | ||
format = options.format || 'default' | ||
function morgan(format, options) { | ||
var fmt = format | ||
var opts = options || {} | ||
if (format && typeof format === 'object') { | ||
opts = format | ||
fmt = opts.format || 'default' | ||
// smart deprecation message | ||
deprecate('morgan(options): use morgan(' + (typeof format === 'string' ? JSON.stringify(format) : 'format') + ', options) instead') | ||
deprecate('morgan(options): use morgan(' + (typeof fmt === 'string' ? JSON.stringify(fmt) : 'format') + ', options) instead') | ||
} | ||
if (format === undefined) { | ||
if (fmt === undefined) { | ||
deprecate('undefined format: specify a format') | ||
} | ||
options = options || {} | ||
// output on request instead of response | ||
var immediate = options.immediate; | ||
var immediate = opts.immediate | ||
// check if log entry should be skipped | ||
var skip = options.skip || function () { return false; }; | ||
var skip = opts.skip || false | ||
// format function | ||
var fmt = compile(exports[format] || format || exports.default) | ||
var formatLine = typeof fmt !== 'function' | ||
? getFormatFunction(fmt) | ||
: fmt | ||
// steam | ||
var buffer = options.buffer | ||
var stream = options.stream || process.stdout | ||
// stream | ||
var buffer = opts.buffer | ||
var stream = opts.stream || process.stdout | ||
@@ -81,38 +97,26 @@ // buffering support | ||
var realStream = stream | ||
var buf = [] | ||
var timer = null | ||
var interval = 'number' == typeof buffer | ||
? buffer | ||
: defaultBufferDuration | ||
// flush interval | ||
var interval = typeof buffer !== 'number' | ||
? defaultBufferDuration | ||
: buffer | ||
// flush function | ||
var flush = function(){ | ||
timer = null | ||
if (buf.length) { | ||
realStream.write(buf.join('')); | ||
buf.length = 0; | ||
} | ||
} | ||
// swap the stream | ||
stream = { | ||
write: function(str){ | ||
if (timer === null) { | ||
timer = setTimeout(flush, interval) | ||
} | ||
buf.push(str); | ||
} | ||
}; | ||
stream = createBufferStream(stream, interval) | ||
} | ||
return function logger(req, res, next) { | ||
req._startAt = process.hrtime(); | ||
req._startTime = new Date; | ||
req._remoteAddress = getip(req); | ||
// request data | ||
req._startAt = undefined | ||
req._startTime = undefined | ||
req._remoteAddress = getip(req) | ||
function logRequest(){ | ||
if (skip(req, res)) { | ||
// response data | ||
res._startAt = undefined | ||
res._startTime = undefined | ||
// record request start | ||
recordStartTime.call(req) | ||
function logRequest() { | ||
if (skip !== false && skip(req, res)) { | ||
debug('skip request') | ||
@@ -122,3 +126,3 @@ return | ||
var line = fmt(exports, req, res) | ||
var line = formatLine(morgan, req, res) | ||
@@ -134,6 +138,10 @@ if (null == line) { | ||
// immediate | ||
if (immediate) { | ||
logRequest(); | ||
// immediate log | ||
logRequest() | ||
} else { | ||
// record response start | ||
onHeaders(res, recordStartTime) | ||
// log when response finished | ||
onFinished(res, logRequest) | ||
@@ -144,64 +152,9 @@ } | ||
}; | ||
}; | ||
} | ||
/** | ||
* Compile `format` into a function. | ||
* | ||
* @private | ||
* @param {Function|String} format | ||
* @return {Function} | ||
*/ | ||
function compile(format) { | ||
if (typeof format === 'function') { | ||
// already compiled | ||
return format | ||
} | ||
if (typeof format !== 'string') { | ||
throw new TypeError('argument format must be a function or string') | ||
} | ||
var fmt = format.replace(/"/g, '\\"') | ||
var js = ' return "' + fmt.replace(/:([-\w]{2,})(?:\[([^\]]+)\])?/g, function(_, name, arg){ | ||
return '"\n + (tokens["' + name + '"](req, res, ' + String(JSON.stringify(arg)) + ') || "-") + "'; | ||
}) + '";' | ||
return new Function('tokens, req, res', js); | ||
}; | ||
/** | ||
* Define a token function with the given `name`, | ||
* and callback `fn(req, res)`. | ||
* | ||
* @public | ||
* @param {String} name | ||
* @param {Function} fn | ||
* @return {Object} exports for chaining | ||
*/ | ||
exports.token = function(name, fn) { | ||
exports[name] = fn; | ||
return this; | ||
}; | ||
/** | ||
* Define a `fmt` with the given `name`. | ||
* | ||
* @public | ||
* @param {String} name | ||
* @param {String|Function} fmt | ||
* @return {Object} exports for chaining | ||
*/ | ||
exports.format = function(name, fmt){ | ||
exports[name] = fmt; | ||
return this; | ||
}; | ||
/** | ||
* Apache combined log format. | ||
*/ | ||
exports.format('combined', ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"') | ||
morgan.format('combined', ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"') | ||
@@ -212,3 +165,3 @@ /** | ||
exports.format('common', ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length]') | ||
morgan.format('common', ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length]') | ||
@@ -219,4 +172,4 @@ /** | ||
exports.format('default', ':remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"'); | ||
deprecate.property(exports, 'default', 'default format: use combined format') | ||
morgan.format('default', ':remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"') | ||
deprecate.property(morgan, 'default', 'default format: use combined format') | ||
@@ -227,3 +180,3 @@ /** | ||
exports.format('short', ':remote-addr :remote-user :method :url HTTP/:http-version :status :res[content-length] - :response-time ms'); | ||
morgan.format('short', ':remote-addr :remote-user :method :url HTTP/:http-version :status :res[content-length] - :response-time ms') | ||
@@ -234,3 +187,3 @@ /** | ||
exports.format('tiny', ':method :url :status :res[content-length] - :response-time ms'); | ||
morgan.format('tiny', ':method :url :status :res[content-length] - :response-time ms') | ||
@@ -241,15 +194,27 @@ /** | ||
exports.format('dev', function(tokens, req, res){ | ||
var color = 32; // green | ||
var status = res.statusCode; | ||
morgan.format('dev', function developmentFormatLine(tokens, req, res) { | ||
// get the status code if response written | ||
var status = res._header | ||
? res.statusCode | ||
: undefined | ||
if (status >= 500) color = 31; // red | ||
else if (status >= 400) color = 33; // yellow | ||
else if (status >= 300) color = 36; // cyan | ||
// get status color | ||
var color = status >= 500 ? 31 // red | ||
: status >= 400 ? 33 // yellow | ||
: status >= 300 ? 36 // cyan | ||
: status >= 200 ? 32 // green | ||
: 0 // no color | ||
var fn = compile('\x1b[0m:method :url \x1b[' + color + 'm:status \x1b[0m:response-time ms - :res[content-length]\x1b[0m'); | ||
// get colored function | ||
var fn = developmentFormatLine[color] | ||
return fn(tokens, req, res); | ||
}); | ||
if (!fn) { | ||
// compile | ||
fn = developmentFormatLine[color] = compile('\x1b[0m:method :url \x1b[' | ||
+ color + 'm:status \x1b[0m:response-time ms - :res[content-length]\x1b[0m') | ||
} | ||
return fn(tokens, req, res) | ||
}) | ||
/** | ||
@@ -259,5 +224,5 @@ * request url | ||
exports.token('url', function(req){ | ||
return req.originalUrl || req.url; | ||
}); | ||
morgan.token('url', function getUrlToken(req) { | ||
return req.originalUrl || req.url | ||
}) | ||
@@ -268,3 +233,3 @@ /** | ||
exports.token('method', function(req){ | ||
morgan.token('method', function getMethodToken(req) { | ||
return req.method; | ||
@@ -277,9 +242,16 @@ }); | ||
exports.token('response-time', function(req, res){ | ||
if (!res._header || !req._startAt) return ''; | ||
var diff = process.hrtime(req._startAt); | ||
var ms = diff[0] * 1e3 + diff[1] * 1e-6; | ||
return ms.toFixed(3); | ||
}); | ||
morgan.token('response-time', function getResponseTimeToken(req, res) { | ||
if (!req._startAt || !res._startAt) { | ||
// missing request and/or response start time | ||
return | ||
} | ||
// calculate diff | ||
var ms = (res._startAt[0] - req._startAt[0]) * 1e3 | ||
+ (res._startAt[1] - req._startAt[1]) * 1e-6 | ||
// return truncated value | ||
return ms.toFixed(3) | ||
}) | ||
/** | ||
@@ -289,8 +261,6 @@ * current date | ||
exports.token('date', function(req, res, format){ | ||
format = format || 'web' | ||
morgan.token('date', function getDateToken(req, res, format) { | ||
var date = new Date() | ||
switch (format) { | ||
switch (format || 'web') { | ||
case 'clf': | ||
@@ -309,5 +279,7 @@ return clfdate(date) | ||
exports.token('status', function(req, res){ | ||
return res._header ? res.statusCode : null; | ||
}); | ||
morgan.token('status', function getStatusToken(req, res) { | ||
return res._header | ||
? String(res.statusCode) | ||
: undefined | ||
}) | ||
@@ -318,3 +290,3 @@ /** | ||
exports.token('referrer', function(req){ | ||
morgan.token('referrer', function getReferrerToken(req) { | ||
return req.headers['referer'] || req.headers['referrer']; | ||
@@ -327,3 +299,3 @@ }); | ||
exports.token('remote-addr', getip); | ||
morgan.token('remote-addr', getip) | ||
@@ -334,6 +306,10 @@ /** | ||
exports.token('remote-user', function (req) { | ||
var creds = auth(req) | ||
var user = (creds && creds.name) || '-' | ||
return user; | ||
morgan.token('remote-user', function getRemoteUserToken(req) { | ||
// parse basic credentials | ||
var credentials = auth(req) | ||
// return username | ||
return credentials | ||
? credentials.name | ||
: undefined | ||
}) | ||
@@ -345,5 +321,5 @@ | ||
exports.token('http-version', function(req){ | ||
return req.httpVersionMajor + '.' + req.httpVersionMinor; | ||
}); | ||
morgan.token('http-version', function getHttpVersionToken(req) { | ||
return req.httpVersionMajor + '.' + req.httpVersionMinor | ||
}) | ||
@@ -354,3 +330,3 @@ /** | ||
exports.token('user-agent', function(req){ | ||
morgan.token('user-agent', function getUserAgentToken(req) { | ||
return req.headers['user-agent']; | ||
@@ -363,6 +339,11 @@ }); | ||
exports.token('req', function(req, res, field){ | ||
return req.headers[field.toLowerCase()]; | ||
}); | ||
morgan.token('req', function getRequestToken(req, res, field) { | ||
// get header | ||
var header = req.headers[field.toLowerCase()] | ||
return Array.isArray(header) | ||
? header.join(', ') | ||
: header | ||
}) | ||
/** | ||
@@ -372,6 +353,15 @@ * response header | ||
exports.token('res', function(req, res, field){ | ||
return (res._headers || {})[field.toLowerCase()]; | ||
}); | ||
morgan.token('res', function getResponseTime(req, res, field) { | ||
if (!res._header) { | ||
return undefined | ||
} | ||
// get header | ||
var header = res.getHeader(field) | ||
return Array.isArray(header) | ||
? header.join(', ') | ||
: header | ||
}) | ||
/** | ||
@@ -400,2 +390,86 @@ * Format a Date in the common log format. | ||
/** | ||
* Compile a format string into a function. | ||
* | ||
* @param {string} format | ||
* @return {function} | ||
* @public | ||
*/ | ||
function compile(format) { | ||
if (typeof format !== 'string') { | ||
throw new TypeError('argument format must be a string') | ||
} | ||
var fmt = format.replace(/"/g, '\\"') | ||
var js = ' return "' + fmt.replace(/:([-\w]{2,})(?:\[([^\]]+)\])?/g, function(_, name, arg) { | ||
return '"\n + (tokens["' + name + '"](req, res, ' + String(JSON.stringify(arg)) + ') || "-") + "' | ||
}) + '";' | ||
return new Function('tokens, req, res', js) | ||
} | ||
/** | ||
* Create a basic buffering stream. | ||
* | ||
* @param {object} stream | ||
* @param {number} interval | ||
* @public | ||
*/ | ||
function createBufferStream(stream, interval) { | ||
var buf = [] | ||
var timer = null | ||
// flush function | ||
function flush() { | ||
timer = null | ||
stream.write(buf.join('')) | ||
buf.length = 0 | ||
} | ||
// write function | ||
function write(str) { | ||
if (timer === null) { | ||
timer = setTimeout(flush, interval) | ||
} | ||
buf.push(str) | ||
} | ||
// return a minimal "stream" | ||
return { write: write } | ||
} | ||
/** | ||
* Define a format with the given name. | ||
* | ||
* @param {string} name | ||
* @param {string|function} fmt | ||
* @public | ||
*/ | ||
function format(name, fmt) { | ||
morgan[name] = fmt | ||
return this | ||
} | ||
/** | ||
* Lookup and compile a named format function. | ||
* | ||
* @param {string} name | ||
* @return {function} | ||
* @public | ||
*/ | ||
function getFormatFunction(name) { | ||
// lookup format | ||
var fmt = morgan[name] || name || morgan.default | ||
// return compiled format | ||
return typeof fmt !== 'function' | ||
? compile(fmt) | ||
: fmt | ||
} | ||
/** | ||
* Get request IP address. | ||
@@ -429,1 +503,25 @@ * | ||
} | ||
/** | ||
* Record the start time. | ||
* @private | ||
*/ | ||
function recordStartTime() { | ||
this._startAt = process.hrtime() | ||
this._startTime = new Date() | ||
} | ||
/** | ||
* Define a token function with the given name, | ||
* and callback fn(req, res). | ||
* | ||
* @param {string} name | ||
* @param {function} fn | ||
* @public | ||
*/ | ||
function token(name, fn) { | ||
morgan[name] = fn | ||
return this | ||
} |
{ | ||
"name": "morgan", | ||
"description": "HTTP request logger middleware for node.js", | ||
"version": "1.5.3", | ||
"version": "1.6.0", | ||
"contributors": [ | ||
@@ -12,11 +12,13 @@ "Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
"dependencies": { | ||
"basic-auth": "~1.0.1", | ||
"basic-auth": "~1.0.2", | ||
"debug": "~2.2.0", | ||
"depd": "~1.0.1", | ||
"on-finished": "~2.2.1" | ||
"on-finished": "~2.3.0", | ||
"on-headers": "~1.0.0" | ||
}, | ||
"devDependencies": { | ||
"istanbul": "0.3.9", | ||
"mocha": "~2.2.4", | ||
"supertest": "~0.15.0" | ||
"istanbul": "0.3.15", | ||
"mocha": "2.2.5", | ||
"split": "1.0.0", | ||
"supertest": "1.0.1" | ||
}, | ||
@@ -23,0 +25,0 @@ "files": [ |
@@ -162,2 +162,13 @@ # morgan | ||
### morgan.compile(format) | ||
Compile a format string into a function for use by `morgan`. A format string | ||
is a string that represents a single log line and can utilize token syntax. | ||
Tokens are references by `:token-name`. If tokens accept arguments, they can | ||
be passed using `[]`, for example: `:token-name[pretty]` would pass the string | ||
`'pretty'` as an argument to the token `token-name`. | ||
Normally formats are defined using `morgan.format(name, format)`, but for certain | ||
advanced uses, this compile function is directly available. | ||
## Examples | ||
@@ -164,0 +175,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
23511
395
316
5
4
2
+ Addedon-headers@~1.0.0
+ Addedee-first@1.1.1(transitive)
+ Addedon-finished@2.3.0(transitive)
+ Addedon-headers@1.0.2(transitive)
- Removedee-first@1.1.0(transitive)
- Removedon-finished@2.2.1(transitive)
Updatedbasic-auth@~1.0.2
Updatedon-finished@~2.3.0