Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add objectMode option for objectMode streams #128

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ morgan('combined', {

Output stream for writing log lines, defaults to `process.stdout`.

##### objectMode

Interpret log lines as objects when writing to `stream`.

#### Predefined Formats

There are various pre-defined formats provided:
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ function morgan (format, options) {
// output on request instead of response
var immediate = opts.immediate

// output to an objectMode stream
var objectMode = opts.objectMode

// check if log entry should be skipped
var skip = opts.skip || false

Expand Down Expand Up @@ -127,6 +130,11 @@ function morgan (format, options) {
}

debug('log request')
if (objectMode) {
stream.write(line)
return
}

stream.write(line + '\n')
};

Expand Down
34 changes: 34 additions & 0 deletions test/morgan.js
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,40 @@ describe('morgan()', function () {
.expect(200, done)
})
})

describe('with objectMode option', function () {
it('should write the literal object value', function (done) {
var cb = after(1, function (err) {
if (err) return done(err)
done()
})

function format (tokens, req, res) {
return {
method: tokens.method(req, res),
url: tokens.url(req, res),
status: tokens.status(req, res)
}
}

var stream = {
write: function (obj) {
assert.equal(obj.method, 'GET')
assert.equal(obj.url, '/')
assert.equal(obj.status, 200)
}
}

var server = createServer(format, {
stream: stream,
objectMode: true
})

request(server)
.get('/')
.expect(200, cb)
})
})
})

describe('morgan.compile(format)', function () {
Expand Down