diff --git a/index.js b/index.js index 4680eae..88d44d7 100644 --- a/index.js +++ b/index.js @@ -79,12 +79,14 @@ function morgan (format, options) { var skip = opts.skip || false // format function - var formatLine = typeof fmt !== 'function' + var isFormatFunction = typeof fmt === 'function' + var formatLine = !isFormatFunction ? getFormatFunction(fmt) : fmt // stream var buffer = opts.buffer + var isStreamDefined = !!opts.stream var stream = opts.stream || process.stdout // buffering support @@ -126,8 +128,13 @@ function morgan (format, options) { return } - debug('log request') - stream.write(line + '\n') + if (opts.enableObjectStream && isStreamDefined && isFormatFunction) { + debug('log request without newline character') + stream.write(line) + } else { + debug('log request') + stream.write(line + '\n') + } }; if (immediate) { diff --git a/package.json b/package.json index 7775fe6..86aa0f1 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "on-headers": "~1.0.1" }, "devDependencies": { - "eslint": "5.9.0", + "eslint": "5.10.0", "eslint-config-standard": "12.0.0", "eslint-plugin-import": "2.14.0", "eslint-plugin-markdown": "1.0.0-rc.0", diff --git a/test/morgan.js b/test/morgan.js index fa5e75a..3f8cfb2 100644 --- a/test/morgan.js +++ b/test/morgan.js @@ -1376,6 +1376,77 @@ describe('morgan()', function () { .expect(200, done) }) }) + + describe('with enableObjectStream option', function () { + it('should write an Object to stream based on the custom format function', function (done) { + var cb = after(2, function (err, res, streamOutput) { + if (err) return done(err) + assert(typeof streamOutput === 'object') + assert(streamOutput.get === 'GET') + done() + }) + + var customStream = { + write: function (streamOutput) { + cb(null, null, streamOutput) + } + } + + var customFormatFunction = function (tokens, req, res) { + return { get: tokens.method(req, res) } + } + + request(createServer(customFormatFunction, { enableObjectStream: true, stream: customStream })) + .get('/fakeEndpoint') + .expect(200, cb) + }) + + it('should write a string to stream when format is not a custom function', function (done) { + var cb = after(2, function (err, res, streamOutput) { + if (err) return done(err) + var masked = streamOutput.replace(/\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+/, '_timestamp_') + assert.strictEqual(masked, res.text + ' - tj [_timestamp_] "GET /fakeEndpoint HTTP/1.1" 200 - "http://localhost/" "my-ua"\n') + done() + }) + + var customStream = { + write: function (streamOutput) { + cb(null, null, streamOutput) + } + } + + request(createServer('default', { enableObjectStream: true, stream: customStream })) + .get('/fakeEndpoint') + .set('Authorization', 'Basic dGo6') + .set('Referer', 'http://localhost/') + .set('User-Agent', 'my-ua') + .expect(200, cb) + }) + + it('should default to process.stdout when stream is not defined', function (done) { + var cb = after(2, function (err, res, line) { + if (err) return done(err) + assert(res.text.length > 0) + assert(typeof line === 'string') + done() + }) + + var stream = createLineStream(function (line) { + cb(null, null, line) + }) + + Object.defineProperty(process, 'stdout', { + value: stream + }) + + var customFormatFunction = function (tokens, req, res) { + return { get: tokens.method(req, res) } + } + request(createServer(customFormatFunction, { enableObjectStream: true, stream: undefined })) + .get('/') + .expect(200, cb) + }) + }) }) describe('morgan.compile(format)', function () {