Skip to content

Commit

Permalink
Use fast-safe-stringify for perf and to support circular refs (#35)
Browse files Browse the repository at this point in the history
* [dist] Use fast-safe-stringify as the default in logform after chatting with @davidmarkclements during JSConf.eu – shaves ~20% off "WinstonObj" benchmarks in pino benchmark suite.

* [test] Assert that we now handle circular references since `fast-stringify-safe` does.

* [fix tiny] s/writeable/writable to be consistent with Node core naming conventions.
  • Loading branch information
indexzero authored Jun 12, 2018
1 parent 038075d commit 3327be2
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 13 deletions.
3 changes: 2 additions & 1 deletion json.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const format = require('./format');
const { MESSAGE } = require('triple-beam');
const jsonStringify = require('fast-safe-stringify');

/*
* function replacer (key, value)
Expand All @@ -20,6 +21,6 @@ function replacer(key, value) {
* to transports in `winston < 3.0.0`.
*/
module.exports = format((info, opts) => {
info[MESSAGE] = JSON.stringify(info, opts.replacer || replacer, opts.space);
info[MESSAGE] = jsonStringify(info, opts.replacer || replacer, opts.space);
return info;
});
3 changes: 2 additions & 1 deletion logstash.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const format = require('./format');
const { MESSAGE } = require('triple-beam');
const jsonStringify = require('fast-safe-stringify');

/*
* function logstash (info)
Expand All @@ -23,6 +24,6 @@ module.exports = format(info => {
}

logstash['@fields'] = info;
info[MESSAGE] = JSON.stringify(logstash);
info[MESSAGE] = jsonStringify(logstash);
return info;
});
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"homepage": "https://github.com/winstonjs/logform#readme",
"dependencies": {
"colors": "^1.2.1",
"fast-safe-stringify": "^2.0.4",
"fecha": "^2.3.3",
"ms": "^2.1.1",
"triple-beam": "^1.2.0"
Expand Down
3 changes: 2 additions & 1 deletion simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const format = require('./format');
const { MESSAGE } = require('triple-beam');
const jsonStringify = require('fast-safe-stringify');

/*
* function simple (info)
Expand All @@ -15,7 +16,7 @@ const { MESSAGE } = require('triple-beam');
* ${level}: ${message} ${JSON.stringify(rest)} otherwise
*/
module.exports = format(info => {
const stringifiedRest = JSON.stringify(Object.assign({}, info, {
const stringifiedRest = jsonStringify(Object.assign({}, info, {
level: undefined,
message: undefined,
splat: undefined
Expand Down
10 changes: 5 additions & 5 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ exports.setupLevels = () => {
};

/*
* Returns a new writeable stream with the specified write function.
* Returns a new writable stream with the specified write function.
* @param {function} write Write function for the specified stream
* @returns {stream.Writeable} A writeable stream instance
* @returns {stream.Writeable} A writable stream instance
*/
exports.writeable = write => (
exports.writable = write => (
new stream.Writable({
write,
objectMode: true
Expand All @@ -41,12 +41,12 @@ exports.infoify = info => {
*/
exports.assumeFormatted = (fmt, info, assertion) => {
return done => {
const writeable = exports.writeable(actual => {
const writable = exports.writable(actual => {
assertion(actual, info);
done();
});

writeable.write(fmt.transform(Object.assign({}, info), fmt.options));
writable.write(fmt.transform(Object.assign({}, info), fmt.options));
};
};

Expand Down
32 changes: 27 additions & 5 deletions test/json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

const assume = require('assume');
const json = require('../json');
const helpers = require('./helpers');
const jsonStringify = require('fast-safe-stringify');
const { assumeFormatted, assumeHasPrototype, writable } = require('./helpers');
const { MESSAGE } = require('triple-beam');

describe('json', () => {
it('json() (default) sets info[MESSAGE]', helpers.assumeFormatted(
it('json() (default) sets info[MESSAGE]', assumeFormatted(
json(),
{ level: 'info', message: 'whatever' },
(info, expected) => {
Expand All @@ -21,7 +22,7 @@ describe('json', () => {
}
));

it('json({ space: 2 }) sets info[MESSAGE]', helpers.assumeFormatted(
it('json({ space: 2 }) sets info[MESSAGE]', assumeFormatted(
json({ space: 2 }),
{ level: 'info', message: '2 spaces 4 lyfe' },
(info, expected) => {
Expand All @@ -33,7 +34,7 @@ describe('json', () => {
}
));

it('json({ replacer }) sets info[MESSAGE]', helpers.assumeFormatted(
it('json({ replacer }) sets info[MESSAGE]', assumeFormatted(
json({
replacer: function onlyLevelAndMessage(key, value) {
if (key === 'filtered') { return undefined; }
Expand All @@ -52,5 +53,26 @@ describe('json', () => {
}
));

it('exposes the Format prototype', helpers.assumeHasPrototype(json));

it('json() can handle circular JSON objects', (done) => {
// Define an info with a circular reference.
const circular = { level: 'info', message: 'has a circular ref ok!', filtered: true };
circular.self = { circular };

const fmt = json();
const stream = writable(info => {
assume(info.level).is.a('string');
assume(info.message).is.a('string');
assume(info.filtered).equals(true);
assume(info.level).equals('info');
assume(info.message).equals('has a circular ref ok!');
assume(info.self.circular).equals(circular);
assume(info[MESSAGE]).equals(jsonStringify(circular));
done();
});

stream.write(fmt.transform(circular, fmt.options));
});

it('exposes the Format prototype', assumeHasPrototype(json));
});

0 comments on commit 3327be2

Please sign in to comment.