Socket
Socket
Sign inDemoInstall

morgan

Package Overview
Dependencies
6
Maintainers
6
Versions
26
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.4.1 to 1.5.0

11

HISTORY.md

@@ -0,1 +1,12 @@

1.5.0 / 2014-11-06
==================
* Add multiple date formats
- `clf` for the common log format
- `iso` for the common ISO 8601 date time format
- `web` for the common RFC 1123 date time format
* Deprecate `buffer` option
* Fix date format in `common` and `combined` formats
* Fix token arguments to accept values with `"`
1.4.1 / 2014-10-22

@@ -2,0 +13,0 @@ ==================

76

index.js

@@ -21,2 +21,12 @@ /*!

/**
* Array of CLF month names.
* @private
*/
var clfmonth = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
]
/**
* Default log buffer duration.

@@ -61,8 +71,10 @@ * @private

// options
// steam
var buffer = options.buffer
var stream = options.stream || process.stdout
, buffer = options.buffer;
// buffering support
if (buffer) {
deprecate('buffer option')
var realStream = stream

@@ -150,3 +162,3 @@ var buf = []

var js = ' return "' + fmt.replace(/:([-\w]{2,})(?:\[([^\]]+)\])?/g, function(_, name, arg){
return '"\n + (tokens["' + name + '"](req, res, "' + arg + '") || "-") + "';
return '"\n + (tokens["' + name + '"](req, res, ' + String(JSON.stringify(arg)) + ') || "-") + "';
}) + '";'

@@ -190,3 +202,3 @@

exports.format('combined', ':remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"')
exports.format('combined', ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"')

@@ -197,3 +209,3 @@ /**

exports.format('common', ':remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :res[content-length]')
exports.format('common', ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length]')

@@ -264,7 +276,18 @@ /**

/**
* UTC date
* current date
*/
exports.token('date', function(){
return new Date().toUTCString();
exports.token('date', function(req, res, format){
format = format || 'web'
var date = new Date()
switch (format) {
case 'clf':
return clfdate(date)
case 'iso':
return date.toISOString()
case 'web':
return date.toUTCString()
}
});

@@ -337,2 +360,24 @@

/**
* Format a Date in the common log format.
*
* @private
* @param {Date} dateTime
* @return {string}
*/
function clfdate(dateTime) {
var date = dateTime.getUTCDate()
var hour = dateTime.getUTCHours()
var mins = dateTime.getUTCMinutes()
var secs = dateTime.getUTCSeconds()
var year = dateTime.getUTCFullYear()
var month = clfmonth[dateTime.getUTCMonth()]
return pad2(date) + '/' + month + '/' + year
+ ':' + pad2(hour) + ':' + pad2(mins) + ':' + pad2(secs)
+ ' +0000'
}
/**
* Get request IP address.

@@ -351,1 +396,16 @@ *

}
/**
* Pad number to two digits.
*
* @private
* @param {number} num
* @return {string}
*/
function pad2(num) {
var str = String(num)
return (str.length === 1 ? '0' : '')
+ str
}

3

package.json
{
"name": "morgan",
"description": "http request logger middleware for node.js",
"version": "1.4.1",
"version": "1.5.0",
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)",

@@ -20,3 +20,2 @@ "contributors": [

"mocha": "~2.0.0",
"should": "~4.1.0",
"supertest": "~0.14.0"

@@ -23,0 +22,0 @@ },

@@ -29,7 +29,2 @@ # morgan

#### buffer
Buffer duration before writing logs to the `stream`, defaults to `false`. When
set to `true`, defaults to `1000 ms`.
#### immediate

@@ -66,3 +61,3 @@

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

@@ -75,3 +70,3 @@

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

@@ -107,16 +102,5 @@

- `:req[header]` ex: `:req[Accept]`
- `:res[header]` ex: `:res[Content-Length]`
- `:http-version`
- `:response-time`
- `:remote-addr`
- `:remote-user`
- `:date`
- `:method`
- `:url`
- `:referrer`
- `:user-agent`
- `:status`
##### Creating new tokens
To define a token, simply invoke `morgan.token()` with the name and a callback function. The value returned is then available as ":type" in this case:
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:
```js

@@ -128,2 +112,56 @@ morgan.token('type', function(req, res){ return req.headers['content-type']; })

##### :date[format]
The current date and time in UTC. The available formats are:
- `clf` for the common log format (`"10/Oct/2000:13:55:36 +0000"`)
- `iso` for the common ISO 8601 date time format (`2000-10-10T13:55:36.000Z`)
- `web` for the common RFC 1123 date time format (`Tue, 10 Oct 2000 13:55:36 GMT`)
If no format is given, then the default is `web`.
##### :http-version
The HTTP version of the request.
##### :method
The HTTP version of the request.
##### :referrer
The Referrer header of the request. This will use the standard mis-spelled Referer header if exists, otherwise Referrer.
##### :remote-addr
The remote address of the request. This will use `req.ip`, otherwise the standard `req.connection.remoteAddress` value (socket address).
##### :remote-user
The user authenticated as part of Basic auth for the request.
##### :req[header]
The given `header` of the request.
##### :res[header]
The given `header` of the response.
##### :response-time
The time between the request coming into `morgan` and when the response headers are written, in milliseconds.
##### :status
The status code of the response.
##### :url
The URL of the request. This will use `req.originalUrl` if exists, otherwise `req.url`.
##### :user-agent
The contents of the User-Agent header of the request.
## Examples

@@ -196,3 +234,3 @@

Sample app that will use custom token formats like changing the ":date"
Sample app that will use custom token formats. This adds an ID to all requests and displays it using the `:id` token.

@@ -202,7 +240,6 @@ ```js

var morgan = require('morgan')
var uuid = require('node-uuid')
// alter :date token format
// ex. 2011-10-05T14:48:00.000Z
morgan.token('date', function () {
return new Date().toISOString()
morgan.token('id', function () {
return req.id
})

@@ -212,3 +249,4 @@

app.use(morgan('combined'))
app.use(assignId)
app.use(morgan(':id :method :url :response-time'))

@@ -218,2 +256,7 @@ app.get('/', function (req, res) {

})
function assignId(req, res, next) {
req.id = uuid.v4()
next()
}
```

@@ -220,0 +263,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc